正在创建文件但没有保存数据?
File is being created but data is not saving?
我查看了几个问题,并确保我关闭了文件,但 none 的数据得到了保存。该文件是在文件系统中创建的,只是显示为空白。我的程序从另一个文件中提取数据并进行一些计算。然后它通过 if 语句发送数据,打印出新文件所需的内容。
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
int tempf, windspeed;
Scanner x;
try {
x = new Scanner(new File("weatherData.txt"));
PrintWriter out = new PrintWriter(new File("numbers.txt"));
while (x.hasNextLine()) {
String[] data = x.nextLine().split("\s+"); // Split the line on space(s)
try {
tempf = Integer.parseInt(data[1]);
//System.out.print(tempf + " ");
} catch (Exception e) {
System.out.println("Invalid/No data for temperature");
tempf = 0;
}
try {
windspeed = Integer.parseInt(data[2]);
//System.out.println(windspeed);
} catch (Exception e) {
//System.out.println("NA");
windspeed = 0;
}
double C1 = -42.379;
double C2 = 2.04901523;
double C3 = 10.14333127;
double C4 = -0.22475541;
double C5 = -.00683783;
double C6 = -5.481717E-2;
double C7 = 1.22874E-3;
double C8 = 8.5282E-4;
double C9 = -1.99E-6;
int celsius = ((tempf-32)*5/9); //Celcius equatin
double windmps = windspeed / 2.23694; // Wind in mps equation
double windchill = 35.74 + 0.6215*tempf + (0.4275*tempf - 35.75) * Math.pow(windspeed, 0.16); // Windchill equation
double windchillc = ((windchill-32)*5/9);
double heatindexf = C1 + (C2 * tempf) + (C3 * windspeed) + (C4 * tempf * windspeed) + (C5 * Math.pow(tempf,2) + (C6 * Math.pow(windspeed,2)) + (C7 * Math.pow(tempf,2) * windspeed) + (C8 * tempf * Math.pow(windspeed,2)) + (C9 * Math.pow(tempf,2) * Math.pow(windspeed,2)));
double heatindexc = ((heatindexf-32)*5/9);
if (tempf <= 50) {
System.out.printf("%20s%20s%20s%20s%20s%20s\n", "Farenheit:","Celsius","Wind Speed(MPH)" ,"Wind Chill(F)" , "Wind Chill(C)" , "Wind Speed(MPS)");
System.out.printf("%20d%20d%20d%20d%20d%20d\n", tempf, celsius,windspeed ,(int)windchill, (int)windchillc, (int)windmps);
}
else if (tempf >= 80) {
System.out.printf("%20s%20s%20s%20s%20s\n", "Farenheit:","Celsius","Humidity %", "Heat Index(F)","Heat Index(C)");
System.out.printf("%20d%20d%20d%20d%20d\n", tempf ,celsius,windspeed , (int)heatindexf, (int)heatindexc);
}
else if (tempf > 50 && tempf < 80) {
System.out.printf("%20s%20s\n","Farenheit:","Celsius");
System.out.printf("%20d%20d\n",tempf,celsius);
}
out.close();
}
} catch (FileNotFoundException e) {
System.out.println("Unable to read file.");
}
}
}
在循环内关闭后无法写入写入器。将其更改为:
PrintStream out = new PrintStream(new FileOutputStream("numbers.txt", true));
System.setOut(out);
while (x.hasNextLine()) {
//Loop body
}
out.close();
另外我把文件改成 PrintStream 然后你可以直接写 system.out 打印到文件。没有明确说明文件写入。
我查看了几个问题,并确保我关闭了文件,但 none 的数据得到了保存。该文件是在文件系统中创建的,只是显示为空白。我的程序从另一个文件中提取数据并进行一些计算。然后它通过 if 语句发送数据,打印出新文件所需的内容。
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
int tempf, windspeed;
Scanner x;
try {
x = new Scanner(new File("weatherData.txt"));
PrintWriter out = new PrintWriter(new File("numbers.txt"));
while (x.hasNextLine()) {
String[] data = x.nextLine().split("\s+"); // Split the line on space(s)
try {
tempf = Integer.parseInt(data[1]);
//System.out.print(tempf + " ");
} catch (Exception e) {
System.out.println("Invalid/No data for temperature");
tempf = 0;
}
try {
windspeed = Integer.parseInt(data[2]);
//System.out.println(windspeed);
} catch (Exception e) {
//System.out.println("NA");
windspeed = 0;
}
double C1 = -42.379;
double C2 = 2.04901523;
double C3 = 10.14333127;
double C4 = -0.22475541;
double C5 = -.00683783;
double C6 = -5.481717E-2;
double C7 = 1.22874E-3;
double C8 = 8.5282E-4;
double C9 = -1.99E-6;
int celsius = ((tempf-32)*5/9); //Celcius equatin
double windmps = windspeed / 2.23694; // Wind in mps equation
double windchill = 35.74 + 0.6215*tempf + (0.4275*tempf - 35.75) * Math.pow(windspeed, 0.16); // Windchill equation
double windchillc = ((windchill-32)*5/9);
double heatindexf = C1 + (C2 * tempf) + (C3 * windspeed) + (C4 * tempf * windspeed) + (C5 * Math.pow(tempf,2) + (C6 * Math.pow(windspeed,2)) + (C7 * Math.pow(tempf,2) * windspeed) + (C8 * tempf * Math.pow(windspeed,2)) + (C9 * Math.pow(tempf,2) * Math.pow(windspeed,2)));
double heatindexc = ((heatindexf-32)*5/9);
if (tempf <= 50) {
System.out.printf("%20s%20s%20s%20s%20s%20s\n", "Farenheit:","Celsius","Wind Speed(MPH)" ,"Wind Chill(F)" , "Wind Chill(C)" , "Wind Speed(MPS)");
System.out.printf("%20d%20d%20d%20d%20d%20d\n", tempf, celsius,windspeed ,(int)windchill, (int)windchillc, (int)windmps);
}
else if (tempf >= 80) {
System.out.printf("%20s%20s%20s%20s%20s\n", "Farenheit:","Celsius","Humidity %", "Heat Index(F)","Heat Index(C)");
System.out.printf("%20d%20d%20d%20d%20d\n", tempf ,celsius,windspeed , (int)heatindexf, (int)heatindexc);
}
else if (tempf > 50 && tempf < 80) {
System.out.printf("%20s%20s\n","Farenheit:","Celsius");
System.out.printf("%20d%20d\n",tempf,celsius);
}
out.close();
}
} catch (FileNotFoundException e) {
System.out.println("Unable to read file.");
}
}
}
在循环内关闭后无法写入写入器。将其更改为:
PrintStream out = new PrintStream(new FileOutputStream("numbers.txt", true));
System.setOut(out);
while (x.hasNextLine()) {
//Loop body
}
out.close();
另外我把文件改成 PrintStream 然后你可以直接写 system.out 打印到文件。没有明确说明文件写入。