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 = 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();
    }

Leave a Comment