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

后端 未结 2 1010
南旧
南旧 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:04

    I would strongly recommend spending some time reading through the Java Trails Tutorials. To answer your specific question, look at Lesson: Exceptions.

    To oversimplify, just wrap the file-handling code in a try...catch block. By example:

    package frp;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    
    public class FileRead {
    
        public static void main(String[] args) {
            try {
                FileReader file = new FileReader(new File("dichromatic.dat"));
                BufferedReader br = new BufferedReader(file);
                String temp = br.readLine();
                while (temp != null) {
                    temp = br.readLine();
                    System.out.println(temp);
                }
                file.close();
            } catch (FileNotFoundException fnfe) {
                System.err.println("File not found: " + fnfe.getMessage() );
            } catch (IOException ioe) {
                System.err.println("General IO Error encountered while processing file: " + ioe.getMessage() );
            }
        }
    }
    

    Note that ideally, your try...catch should wrap the smallest possible unit of code. So, wrap the FileReader separately, and "fail-fast" if the file isn't found, and wrap the readLine loop in its own try...catch. For more examples and a better explanation of how to deal with exceptions, please reference the link I provided at the top of this answer.

    Edit: issue of file path

    Not finding the file has to do with the location of the file relative to the root of the project. In your original post, you reference the file as "dichromatic.dat" but relative to the project root, it is in "src/frp/dichromatic.dat". As rpax recommends, either change the string that points to the file to properly reference the location of the file relative to the project root, or move the file to project root and leave the string as-is.

    0 讨论(0)
  • 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();
        }
    
    0 讨论(0)
提交回复
热议问题