Using java.io library in eclipse so FileInputStream can read a dat file

1. Without changing your code, you must place the file in the project’s root folder. Otherwise, reference it as src/frp/dichromatic.dat 2. Doing something like this: public static void main(String[] args) { FileReader file = null; try { file = new FileReader(new File(“dichromatic.dat”)); } catch (FileNotFoundException e1) { System.err.println(“File dichromatic.dat not found!”); e1.printStackTrace(); } BufferedReader br … Read more

How to get just the parent directory name of a specific file

Use File‘s getParentFile() method and String.lastIndexOf() to retrieve just the immediate parent directory. Mark’s comment is a better solution thanlastIndexOf(): file.getParentFile().getName(); These solutions only works if the file has a parent file (e.g., created via one of the file constructors taking a parent File). When getParentFile() is null you’ll need to resort to using lastIndexOf, … Read more

java IO to copy one File to another

No, there is no built-in method to do that. The closest to what you want to accomplish is the transferFrom method from FileOutputStream, like so: FileChannel src = new FileInputStream(file1).getChannel(); FileChannel dest = new FileOutputStream(file2).getChannel(); dest.transferFrom(src, 0, src.size()); And don’t forget to handle exceptions and close everything in a finally block.

how to File.listFiles in alphabetical order?

The listFiles method, with or without a filter does not guarantee any order. It does, however, return an array, which you can sort with Arrays.sort(). File[] files = XMLDirectory.listFiles(filter_xml_files); Arrays.sort(files); for(File _xml_file : files) { … } This works because File is a comparable class, which by default sorts pathnames lexicographically. If you want to … Read more