简体   繁体   中英

Attempting to write read and write files with java, results in blank output files

I have a file 'A.txt' which consists of 60 lines. Each line contains a single number from 1 to 60. I also have a folder DD that contains 2 files, labelled 'DD1.txt' and 'DD2.txt' File DD1 has 3 lines. Each line of DD1 has 1.1, 1.2 and 1.3 . Similarly file DD2 also has 3 lines. Each line of DD2 has 2.1, 2.2, 2.3.

What I am trying to do is first read the first line of the A.txt, which calculates with all the lines of DD1 and writes the calculated value, and calculates with all the lines of DD2 and writes the output value. Thus I have 60 X 2 = 120 files to be created. Each file should have 2 lines. But I am getting only 2 files which is inside blank at the moment.

This is what I have tried so far.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class Sample {
    private static final String FILEA = "E:\\SP\\A.txt";
    private static String FILEH = "E:\\SP\\DD\\DD";

    private static String FILER = "E:\\SP\\Result\\Result";
    private static final String FILEEXE = ".txt";
    private static BufferedReader br = null, br1 = null, br2 = null;

    private static BufferedWriter bw = null;
    private static FileWriter fw = null;
    public static void main(String[] args) throws NumberFormatException,IOException, InterruptedException
    {
        final double C1 = -3.75, C2 = 1.14;
        double T = 0, R = 0, E = 0, Efficiency = 1;

        System.out.println("Enter the value of Efficiency:");
        br = new BufferedReader(new InputStreamReader(System.in));
        Efficiency = Double.parseDouble(br.readLine());

        try {
            String sCurrentLine, sCurrentLine1, sCurrentLine2;
            int i = 0;
            br1 = new BufferedReader(new FileReader(FILEA));

            while ((sCurrentLine = br1.readLine()) != null)
            {
                i++;
                String FILEH1 = FILEH + i + FILEEXE;
                String FILER1 = FILER + i + FILEEXE;
                br2 = new BufferedReader(new FileReader(FILEH1));
                bw = new BufferedWriter(new FileWriter(FILER1));
                int count = 0;
                while ((sCurrentLine1 = br2.readLine()) != null)
                {

                    T = Double.parseDouble(sCurrentLine1);
                    R = Double.parseDouble(sCurrentLine);

                    E = R * 24 * (T * 0.024);
                    bw.write(Double.toString(E));
                    bw.newLine();
                }
            }
        } catch (Exception e2) {
            // TODO: handle exception
        }//end trycatch
    }
}

After you made your calculation inside the while loop try bw.write("\\r\\n"); Then after the while loop try closing the write as bw.close(); Hope this works for you.

   bw.write(Double.toString(E));
   bw.write("\r\n");
}
bw.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM