尝试使用java编写读写文件会导致空白输出文件

时间:2017-03-23 01:55:44

标签: java

我有一个'A.txt'文件,由60行组成。每行包含1到60之间的单个数字。我还有一个包含2个文件的文件夹DD,标记为“DD1.txt”和“DD2.txt” 文件DD1有3行。 DD1的每一行都有1.1,1.2和1.3。类似地,文件DD2也有3行。每行DD2都有2.1,2.2,2.3。

我要做的是首先读取A.txt的第一行,它用DD1的所有行计算并写入计算的值,并用DD2的所有行计算并写入输出值。因此我要创建60个X 2 = 120个文件。每个文件应该有2行。但我现在只收到2个空白的文件。

这是我到目前为止所尝试过的。

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
    }
}

1 个答案:

答案 0 :(得分:1)

在while循环中进行计算后,尝试bw.write(“\ r \ n”);然后在while循环之后尝试关闭write为bw.close();希望这对你有用。

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