java-io

NullPointerException when using java.io.File

这一生的挚爱 提交于 2019-12-29 01:53:12
问题 I am trying to use this program to count all the files in the D:\ drive but it is throwing an exception when I run it. package lmh; import java.io.File; public class FileList { static int fileNum = 0; static int directoryNum = 0; static int cannotRead = 0; public static void main(String[] args) { File f = new File("e:/"); printFileStructure(f); System.out.println("result:"); System.out.println("file number:" + fileNum); System.out.println("directory number:" + directoryNum); System.out

How to use flush() for PrintWriter

独自空忆成欢 提交于 2019-12-28 04:23:10
问题 I have some codes like this: PrintWriter pw = new PrintWriter(new BufferedReader(....)); for(int i=0; i<10; i++) { pw.println("a"); pw.flush();// flush each time when println()? } pw.close(); Is the flush() in each 'for' statement necessarily? I heard that flush() would auto invoke when invoke close() . If I write code like this: PrintWriter pw = new PrintWriter(new BufferedReader(....), true); and I wouldn't write pw.flush() anymore? Thanks. 回答1: flush() is probably not required in your

Converting transparent gif / png to jpeg using java

风流意气都作罢 提交于 2019-12-28 01:53:07
问题 I'd like to convert gif images to jpeg using Java. It works great for most images, but I have a simple transparent gif image: Input gif image http://img292.imageshack.us/img292/2103/indexedtestal7.gif [In case the image is missing: it's a blue circle with transparent pixels around it] When I convert this image using the following code: File file = new File("indexed_test.gif"); BufferedImage image = ImageIO.read(file); File f = new File("indexed_test.jpg"); ImageIO.write(image, "jpg", f); This

Copy database from assets folder [duplicate]

≡放荡痞女 提交于 2019-12-26 20:29:05
问题 This question already has answers here : Can't copy SQLite database from assets (4 answers) Closed 5 years ago . I am creating a Dictionary application which uses an existing sqlite database. I have placed my database in assets folder and I am using the following code to copy the database when the app is started for first time. (I have borrowed the idea from this post) DatabaseHelper.java public class DatabaseHelper { private static String DB_PATH = ""; private static String DB_NAME = "abc

java.io.File delete() failing in Windows 7 (java newbie)

允我心安 提交于 2019-12-26 05:18:25
问题 I'm new to Java, just trying to make a simple utility to move, copy & delete some wav files on my pc, but java.io.File delete() fails. The wav files in question have read-only unchecked (in windows explorer) but File canWrite() returns false & setWritable(true) fails. I must be doing something stupid because nobody seems to have had this problem before? 回答1: You are using a relative path and you are not in the directory you think you are. Specify absolute paths or determine the current path

By java how to count file numbers in a directory without list()

浪尽此生 提交于 2019-12-25 04:21:31
问题 Without using file.list() or Files.list(path) , how to count file numbers in a directory? I just want a number, no detail. Given me a quick way please. 回答1: If your only concern is to not create a List<File> you might use the Stream API . long count = Files.list(Paths.get(path)) .filter(p -> p.toFile().isFile()) .count(); System.out.println("count = " + count); edit The snippet is not meant to be fast. It was only provied for the requirement not to use list() or listFiles() . ;-) Following a

Pulling this custom readDataFile function into Eclipse to print .dat file data to console

核能气质少年 提交于 2019-12-25 01:55:33
问题 Goal: Get the data from a .dat file and print it to the console in Eclipse Resources : fpfret.java and PointF.java and dichromatic.dat I have resolved all my issues and have just a few console errors, here's my code and my question is: How do I add the getCodeBase() method? package frp3; import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.EOFException; import java.net.URL; import java.util.Vector; public class FileRead { public static void main(String[] args) {

Java readLine() not working properly

北战南征 提交于 2019-12-24 03:12:42
问题 I'm writing a translator program that reads the translations from a text file. This method reads from the text file and adds it to a Map object that i'm using for the dictionary. public Map<String, String> loadTranslations() throws IOException { Map<String, String> translations = new HashMap<String, String>(); BufferedReader in = null; try { in = new BufferedReader(new FileReader(System.getProperty("user.dir") + "\\resource\\translations.txt")); englWord = in.readLine(); frenWord = in

Why are results of path.toString() failing to show all characters on Linux but ok on windows

谁说胖子不能爱 提交于 2019-12-24 00:51:08
问题 In my Java code I use a FileVisitor to traverse a filesystem and creating a structure of Paths, then later on this is converted to a json object for rendering in html. Running on Windows it runs okay even against a linux filesystem, running on Linux against the same (now local) filesystem it fails to render special characters properly when call toString() on a path i.e Windows debug output CreateFolderTree:createJsonData:SEVERE: AddingNode(1):Duarte Lôbo- Requiem and html displays ok as

How do you write to a log file in Clojure, using core.async?

◇◆丶佛笑我妖孽 提交于 2019-12-24 00:40:08
问题 I want to use core.async as a logger that writes out to a file, so I created a test.txt file, stuck it in my resources folder and wrote this code: (use 'clojure.java.io) (use 'clojure.core.async) (def print-chan (chan)) (go (loop [] (when-let [v (<! print-chan)] (with-open [wrtr (writer "resources/test.txt" :append true)] (.write wrtr v)) (recur)))) (>!! print-chan 42) When I run this, however, I find that it will only replace what is in the file, and not append to it. Also, sometimes the