【发布时间】:2016-06-24 14:21:17
【问题描述】:
我正在尝试创建一个文本 (numbers.txt) 文件,用户指示他们可以在其中保存文件。我知道
File directory = new File("/home/jon/somewhere");
File fullPath = new File(directory, fileName);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(
(new FileOutputStream(fullPath), charSet));
try {
writer.write("\n");
writer.write("work");
} finally {
writer.close();
}
需要插入,但我不知道如何让它在该位置创建 .txt 文件,该位置是用户通过扫描仪定义的。到目前为止,这是我的代码,直到我期望输入上面的代码为止。如果需要此信息,请使用 Eclipse IDE。
代码的重点是将数字保存到txt文件中,然后拉回来读取,这样就可以计算出来了。
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.Scanner;
import java.io.Serializable;
import java.lang.SecurityException;
import java.util.Formatter;
public class StrangeAverage implements Serializable{
/**
* @param args
*/
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int fails = 0;
do {
System.out.printf("Please enter a directory where you can save files, in case of error/shutdown: ");
Path path = Paths.get(input.nextLine());
if (Files.isDirectory(path))
{
System.out.println("Using Directory " +path);
break;
}
else
{
System.out.println("That's not an open directory.");
fails++;
}
}while(fails < 5);
if (fails == 5)
{
System.out.print("Too many failed attempts. Exiting...");
return;
}
if (fails < 5)
{
System.out.println("Valid Directory.");
}
System.out.printf("Please enter ten integers:");
【问题讨论】:
标签: javascript java string file text-files