java-io

how to File.listFiles in alphabetical order?

a 夏天 提交于 2019-11-27 07:21:18
I've got code as below: class ListPageXMLFiles implements FileFilter { @Override public boolean accept(File pathname) { DebugLog.i("ListPageXMLFiles", "pathname is " + pathname); String regex = ".*page_\\d{2}\\.xml"; if(pathname.getAbsolutePath().matches(regex)) { return true; } return false; } } public void loadPageTrees(String xml_dir_path) { ListPageXMLFiles filter_xml_files = new ListPageXMLFiles(); File XMLDirectory = new File(xml_dir_path); for(File _xml_file : XMLDirectory.listFiles(filter_xml_files)) { loadPageTree(_xml_file); } } The FileFilter is working nicely, but listFiles() seems

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

梦想与她 提交于 2019-11-27 06:42:42
问题 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

Java: opening and reading from a file without locking it

拜拜、爱过 提交于 2019-11-27 05:57:22
问题 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

Should I always wrap an InputStream as BufferedInputStream?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 05:35:14
问题 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; 回答1: 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

How to check whether an OutputStream is closed

大憨熊 提交于 2019-11-27 03:44:19
问题 Is there anyway to check whether an OutputStream is closed without attempting to write to it and catching the IOException ? For example, consider the following contrived method: public boolean isStreamClosed(OutputStream out){ if( /* stream isn't closed */){ return true; }else{ return false; } } What could you replace /* stream isn't closed */ with? 回答1: The underlying stream may not know it its closed until you attempt to write to it (e.g. if the other end of a socket closes it) The simplest

Java Scanner(File) misbehaving, but Scanner(FIleInputStream) always works with the same file

不羁的心 提交于 2019-11-27 02:41:53
问题 I am having weird behavior with Scanner. It will work with a particular set of files I am using when I use the Scanner(FileInputStream) constructor, but it won't with the Scanner(File) constructor. Case 1: Scanner(File) Scanner s = new Scanner(new File("file")); while(s.hasNextLine()) { System.out.println(s.nextLine()); } Result: no output Case 2: Scanner(FileInputStream) Scanner s = new Scanner(new FileInputStream(new File("file"))); while(s.hasNextLine()) { System.out.println(s.nextLine());

Best way to Pipe InputStream to OutputStream [duplicate]

房东的猫 提交于 2019-11-27 02:04:44
问题 This question already has answers here : Easy way to write contents of a Java InputStream to an OutputStream (22 answers) Closed 2 years ago . I was to trying to find the best way to pipe the InputStream to OutputStream. I don't have an option to use any other libraries like Apache IO. Here is the snippet and output. import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.channels

Java. How to append text to top of file.txt [duplicate]

偶尔善良 提交于 2019-11-26 22:56:35
问题 This question already has answers here : Writing in the beginning of a text file Java (5 answers) Closed 5 years ago . I need to add text to beggining of text file via Java. For example I have test.txt file with data: Peter John Alice I need to add(to top of file): Jennifer It should be: Jennifer Peter John Alice I have part of code, but It append data to end of file, I need to make It that added text to top of file: public static void irasymas(String irasymai){ try { File file = new File(

BufferedReader vs Console vs Scanner

荒凉一梦 提交于 2019-11-26 22:33:07
问题 Hi I'm new to Java and I would like to know what is the best choice to read a user Input in the console, as far as I know there are 3 ways to do it: Console console = System.console(); BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); Scanner reader = new Scanner(System.in); Which one should I choose? Why that one and not the other ones? 回答1: BufferedReader Since Java 1.1 Throws checked exceptions Can read chars, char arrays, and lines Fast Scanner Since Java 1.5

Recursively find all text files in directory

旧街凉风 提交于 2019-11-26 21:46:59
问题 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