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

后端 未结 2 1086
南旧
南旧 2020-12-02 02:41
  • Goal: Print the data from a .dat file to the console using Eclipse.
    • (Long-Term Goal): Executable that I can pass a .dat fil
2条回答
  •  庸人自扰
    2020-12-02 03:27

    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 = new BufferedReader(file);
            String line;
            try {
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
    
            } catch (IOException e) {
                System.err.println("Error when reading");
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        System.err.println("Unexpected error");
                        e.printStackTrace();
                    }
                }
            }
        }
    

    3. Creation of a new txt file "formatted". In this example, the formatting will be settings the characters to uppercase.

    public static void main(String[] args) {
            FileReader file = null;
            BufferedWriter bw = null;
            File outputFile = new File("output.formatted");
            try {
                file = new FileReader(new File("dichromatic.dat"));
            } catch (FileNotFoundException e1) {
                System.err.println("File dichromatic.dat not found!");
                e1.printStackTrace();
            }
            try {
                bw = new BufferedWriter(new FileWriter(outputFile));
            } catch (IOException e1) {
                System.err.println("File is not writtable or is not a file");
                e1.printStackTrace();
            }
            BufferedReader br = new BufferedReader(file);
            String line;
            String lineformatted;
            try {
                while ((line = br.readLine()) != null) {
                    lineformatted = format(line);
                    bw.write(lineformatted);
                    // if you need it
                    bw.newLine();
                }
    
            } catch (IOException e) {
                System.err.println("Error when processing the file!");
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        System.err.println("Unexpected error");
                        e.printStackTrace();
                    }
                }
                if (bw != null) {
                    try {
                        bw.close();
                    } catch (IOException e) {
                        System.err.println("Unexpected error");
                        e.printStackTrace();
                    }
                }
            }
        }
    
        public static String format(String line) {
            // replace this with your needs
            return line.toUpperCase();
        }
    

提交回复
热议问题