import javax.swing.*;
import java.io.*;
public class FileIO{
public static void main (String args [])throws IOException{
String input = JOptionPane.showInputDialog("Enter FileName: ");
File x = new File(input);
File f = new File ("d:\\aa\\bb\\");
f.mkdirs();
FileWriter text = new FileWriter (f + "\\grades.txt");
text.write("90");
text.write("91");
text.write("92");
text.write("93");
text.write("94");
text.close();
if (f.exists()){
System.out.println(f.getPath() + " exist");
} else {
System.out.println(f.getPath() + " does not exist");
}
//getting the average of the input grades from grades.txt
/* int sum = 0, ctr=0;
while(val != null){
ctr++;
sum += Integer.parseInt(val);
val = x.readLine();
}
System.out.println("Ave = " + sum/ctr);
f.close();*/
}
}
您好。我想请求你的帮助。我需要检查用户输入的文件是否在文件夹中。然后使用其中的内容为下一组代码计算。例如,在记事本上输入了成绩列表。例如:
grades.txt: 90 91 92 93 94
示例输出:
grades.txt存在于目录
中平均成绩:Ave = 92
答案 0 :(得分:0)
使用java.io.File:
String textFieldValue = testField.getText();
File f = new File(textFieldValue );
if(f.exists() && !f.isDirectory()) {
System.out.println(f.getPath() + " exist");
} else {
System.out.println(f.getPath() + " does not exist");
}
答案 1 :(得分:0)
不确定你得到了什么,但是这个:
File f = new File ("d:\\aa\\bb\\");
f.mkdirs();
FileWriter text = new FileWriter (f + "\\grades.txt");
将尝试为文件d:\aa\bb\\grades.txt
创建FileWriter。请注意\\
。而是使用:
FileWriter text = new FileWriter (new File(f, "grades.txt"));