java-io

Use of FilenameFilter

核能气质少年 提交于 2019-11-29 03:34:34
I have a directory: File dir = new File(MY_PATH); I would like to list all the files whose name is indicated as integer numbers strings, e.g. "10", "20". I know I should use: dir.list(FilenameFilter filter); How to define my FilenameFilter ? P.S. I mean the file name could be any integer string, e.g. "10" or "2000000" or "3452345". No restriction in the number of digits as long as the file name is a integer string. You should override accept in the interface FilenameFilter and make sure that the parameter name has only numeric chars. You can check this by using matches : String[] list = dir

Should I always wrap an InputStream as BufferedInputStream?

走远了吗. 提交于 2019-11-28 18:35:20
Does it make sense to always wrap an InputStream as BufferedInputStream, when I know whether the given InputStream is something other than buffered? For e.g: InputStream is = API.getFromSomewhere() if(!(is instanceof BufferedInputStream)) return new BufferedInputStream(is); return is; Does it make sense to always wrap an InputStream as BufferedInputStream, when I know whether the given InputStream is something other than buffered? No. It makes sense if you are likely to perform lots of small reads (one byte or a few bytes at a time), or if you want to use some of the higher level functionality

Java I/O streams; what are the differences?

限于喜欢 提交于 2019-11-28 16:25:28
java.io has many different I/O streams, (FileInputStream, FileOutputStream, FileReader, FileWriter, BufferedStreams... etc.) and I am confused in determining the differences between them. What are some examples where one stream type is preferred over another, and what are the real differences between them? Andrew Hare This is a big topic! I would recommend that you begin by reading I/O Streams : An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory

Create a directory if it does not exist and then create the files in that directory as well

断了今生、忘了曾经 提交于 2019-11-28 16:10:02
Condition is if directory exists it has to create files in that specific directory with out creating a new directory. The below code only creating a file with new directory but not for existing directory . For example the directory name would be like "GETDIRECTION" String PATH = "/remote/dir/server/"; String fileName = PATH.append(id).concat(getTimeStamp()).append(".txt"); String directoryName = PATH.append(this.getClassName()); File file = new File(String.valueOf(fileName)); File directory = new File(String.valueOf(directoryName)); if(!directory.exists()){ directory.mkdir(); if(!file.exists()

implements Closeable or implements AutoCloseable

独自空忆成欢 提交于 2019-11-28 15:21:58
I'm in the process of learning Java and I cannot find any good explanation on the implements Closeable and the implements AutoCloseable interfaces. When I implemented an interface Closeable , my Eclipse IDE created a method public void close() throws IOException . I can close the stream using pw.close(); without the interface. But, I cannot understand how I can implement the close() method using the interface. And, what is the purpose of this interface? Also I would like to know: how can I check if IOstream was really closed? I was using the basic code below import java.io.*; public class

NullPointerException when using java.io.File

此生再无相见时 提交于 2019-11-28 14:29:34
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.println("cannot rend:" + cannotRead); } public static void printFileStructure(File f) { File[] files = f

Inserting text in middle using RandomAccessFile removes some text after that

非 Y 不嫁゛ 提交于 2019-11-28 14:19:44
My Sample Code String line = null; RandomAccessFile file = new RandomAccessFile("D:/mahtew.txt", "rw"); System.out.println(file.getFilePointer()); while((line = file.readLine()) != null){ System.out.println(line); System.out.println(file.getFilePointer()); if(line.contains("Text to be appended with")){ file.seek(file.getFilePointer()); file.write(" new text has been appended".getBytes()); break; } } file.close(); demo.txt before execution one two three Text to be appended with five six seven eight nine ten demo.txt after execution one two three Text to be appended with new text has been

How to find out which line separator BufferedReader#readLine() used to split the line?

人盡茶涼 提交于 2019-11-28 12:01:44
I am reading a file via the BufferedReader String filename = ... br = new BufferedReader( new FileInputStream(filename)); while (true) { String s = br.readLine(); if (s == null) break; ... } I need to know if the lines are separated by '\n' or '\r\n' is there way I can find out ? I don't want to open the FileInputStream so to scan it initially. Ideally I would like to ask the BufferedReader since it must know. I am happy to override the BufferedReader to hack it but I really don't want to open the filestream twice. Thanks, Note: the current line separator (returned by System.getProperty("line

Recursively find all text files in directory

烂漫一生 提交于 2019-11-28 11:47:43
I am trying to get the names of all of the text files in a directory. If the directory has subdirectories then I also want to get any text files in those as well. I am not sure how to make the process continue for any number of subdirectories. Right now the code below just gets all the text files in the current directory and and subdirectories in the directory. For each subdirectory found, it also finds any text files and deeper subdirectories. The problem is that if those deeper subdirectories have yet deeper subdirectories then I am not finding all the text files. This seems to be a problem

Java: opening and reading from a file without locking it

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 11:09:33
I need to be able to mimic 'tail -f' with Java. I'm trying to read a log file as it's being written by another process, but when I open the file to read it, it locks the file and the other process can't write to it anymore. Any help would be greatly appreciated! Here is the code that I'm using currently: public void read(){ Scanner fp = null; try{ fp = new Scanner(new FileReader(this.filename)); fp.useDelimiter("\n"); }catch(java.io.FileNotFoundException e){ System.out.println("java.io.FileNotFoundException e"); } while(true){ if(fp.hasNext()){ this.parse(fp.next()); } } } Rebuilding tail is