java-io

BufferedReader, detecting if there is text left to read

拟墨画扇 提交于 2019-11-30 05:28:07
问题 I'm running a thread and everytime it runs, It should be checking to see if there is a new line to read from the BufferedReader although, it gets stuck waiting for a line to exist, thus halting the entire code. if((inputLine = bufferedReader.readLine()) != null){ System.out.println(inputLine); JOptionPane.showMessageDialog(null, inputLine); } Is there a way to better check if there is text in a BufferedReader to be read? 回答1: No, there's no easy way to do that. BufferedReader has a ready call

how to create a file with world readable permission under subdirectory of files directory

被刻印的时光 ゝ 提交于 2019-11-30 04:52:41
I need to create files under myapp/files/subdir with global permission in my application. I do this because I use external applications to open some files Using this FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE); creates file only under files folder. Using File dir=new File(Constants.TASK_DIRECTORY); dir.mkdirs(); File file=new File(dir, FILENAME); file.createNewFile(); FileOutputStream fos=new FileOutputStream(file); creates files under subdirectories but with private permissions. I need to find a way to compose those both to create a file in a subdirectory to

Using Java's File.delete() method

馋奶兔 提交于 2019-11-29 15:43:31
When I used the File.delete() method to delete a file, where is the deleted file? I am using a Mac and I don't see the file in the Trash. I want to know where the file is being stored at? Or if it is permanently gone. Thanks, It's gone. The trash bin is just a temporary place where files are put before being deleted, when you "delete" them through the OS. In most filesystems, however, deleting a file only removes the pointer to it from the system's list of files. The actual data may sit on the harddrive for a significant amount of time until it is overwritten. There are file recovery tools

How to list only N files in directory using java

我们两清 提交于 2019-11-29 14:40:25
If I have a directory contains a lot of files (about 1000 file). Some of these files named .processed and other not. How can I list only 10 unprocessed files. I am using this code to filter the processed file. File[] inputFileList = inputDirectory.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return !pathname.getName().endsWith(".processed"); } }); But if the number of un-processed files is huge, this may lead to memory error. so I need to read a limited number of files each time the application will run. Which is why you should use java.nio.file. Using Java 8:

Java read file got a leading BOM [  ]

荒凉一梦 提交于 2019-11-29 14:22:26
I am reading a file containing keywords line by line and found a strange problem. I hope lines that following each other if their contents are the same, they should be handled only once. Like sony sony only the first one is getting processed. but the problems is, java doesn't treat them as equals. INFO: [, s, o, n, y] INFO: [s, o, n, y] My code looks like the following, where's the problem? FileReader fileReader = new FileReader("some_file.txt"); BufferedReader bufferedReader = new BufferedReader(fileReader); String prevLine = ""; String strLine while ((strLine = bufferedReader.readLine()) !=

Java File.exists() versus File.isFile()

社会主义新天地 提交于 2019-11-29 11:21:05
I'm unable to think of a realistic use case for the method java.io.File.exists() or its equivalent in Java 7 java.nio.file.Files.exists(Path) . It seems that isFile() or isDirectory() would be preferable in all cases (or canRead() , canWrite() , etc.). For example, in How do I check if a file exists in Java? , the accepted answer seems silly, as the second answer points out. Can anyone give an example where it's useful to know that a thing exists, without knowing whether the thing is a file or directory? EDIT: I understand what File.exists() does. My question is, when would that functionality

hadoop java.io.IOException: while running namenode -format

别等时光非礼了梦想. 提交于 2019-11-29 07:50:00
问题 I ran namenode -format.This is my output. I tried changing the file permissions chmod 777 hadoop. I believe this line is the error ERROR namenode.NameNode: java.io.IOException: Cannot create directory /your/path/to/hadoop/tmp/dir/hadoop-hadoop/dfs/name/current adoop@alexander-desktop:/usr/local/hadoop/bin$ ./hadoop namenode -format 12/07/03 17:03:56 INFO namenode.NameNode: STARTUP_MSG: /************************************************************ STARTUP_MSG: Starting NameNode STARTUP_MSG:

Is PrintWriter buffered?

非 Y 不嫁゛ 提交于 2019-11-29 07:18:45
I know that the PrintWriter is really good if we want to write formatted data, and I also know the use of BufferedWriter to improve IO performance. But I tried something like this, PrintWriter pw = new PrintWriter(System.out); pw.println("Statement 1"); pw.println("Statement 2"); //pw.flush(); I observed that when the flush method is commented there is no output, but when I uncomment it, I get the desired output. This is only possible if the PrintWriter is buffered. If so, then what is the point of wrapping a PrintWriter using a BufferedWriter and then writing it? Though the javadoc doesn't

How to find sub-directories in a directory/folder?

纵然是瞬间 提交于 2019-11-29 05:31:33
I'm looking for a way to get all the names of directories in a given directory, but not files. For example, let's say I have a folder called Parent , and inside that I have 3 folders: Child1 Child2 and Child3 . I want to get the names of the folders, but don't care about the contents, or the names of subfolders inside Child1, Child2, etc. Is there a simple way to do this? You can use String[] directories = file.list() to list all file names, then use loop to check each sub-files and use file.isDirectory() function to get subdirectories. For example: File file = new File("C:\\Windows"); String[

Delete Files with same Prefix String using Java

浪子不回头ぞ 提交于 2019-11-29 05:26:19
I have around 500 text files inside a directory with a same prefix in their filename say dailyReport_ . The latter part of the file is the date of the file. (For eg. dailyReport_08262011.txt , dailyReport_08232011.txt ) I want to delete these files using a Java procedure (I could go for a shell script and add it a job in the crontab but the application is meant to used by laymen). I can delete one single file using something like this try{ File f=new File("dailyReport_08232011.txt"); f.delete(); } catch(Exception e){ System.out.println(e); } but can I delete the files having a certain prefix