Java Can';使用扫描仪读取以前创建的文件时找不到文件
我正在尝试编写一个程序,该程序获取在以前的程序中使用Java Can';使用扫描仪读取以前创建的文件时找不到文件,java,macos,printwriter,Java,Macos,Printwriter,我正在尝试编写一个程序,该程序获取在以前的程序中使用PrintWriter创建的文档,然后对结果进行排序。我很难运行第一个程序,因为我有一个iMac,路径不仅仅是C://FileName.text。第一个程序创建文件并将其打印到文档中。当我去Finder并在文档下查看时,我可以看到它。然而,尝试使用我在第一个程序中使用的相同格式来定位文件似乎不起作用。我需要能够将第一个程序创建的文档读入第二个程序,然后对结果进行排序。我想简化一下这个问题:我用一个程序创建了一个文本文件,现在我需要能够使用第二个
PrintWriter
创建的文档,然后对结果进行排序。我很难运行第一个程序,因为我有一个iMac,路径不仅仅是C://FileName.text
。第一个程序创建文件并将其打印到文档中。当我去Finder并在文档下查看时,我可以看到它。然而,尝试使用我在第一个程序中使用的相同格式来定位文件似乎不起作用。我需要能够将第一个程序创建的文档读入第二个程序,然后对结果进行排序。我想简化一下这个问题:我用一个程序创建了一个文本文件,现在我需要能够使用第二个程序来读取刚刚创建的文档,这样我就可以整理出结果。我不确定为什么程序找不到新文件,我可以在文档中看到它
这是它给出的错误:
Exception in thread "main" java.io.FileNotFoundException: /Users/myName/user.home.UnsortedDocument.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.util.Scanner.<init>(Scanner.java:656)
at Sorting.readIn(Sorting.java:14)
at Sorting.main(Sorting.java:35)
这是我遇到麻烦的新程序的一部分,该部分在新创建的文档中读取:
public Scramble(){
}
public void readIn()throws Exception {
Scanner stdln = new Scanner(new File(System.getProperty("user.home"),"SortedDocument.txt"));
int i = 0;
while (stdln.hasNextLine()){
line[i] = stdln.nextLine();
i++;
}
}
public void mixUp()throws Exception{
PrintWriter out = new PrintWriter("user.home.UnsortedDocument.txt");
int inc = 239;
int counter = 0;
while (inc>0){
int r = rand.nextInt(inc);
for (int i = 0; counter!=r; i++){
if (line[i]!= null){
counter++;
}
if (counter==r){
out.println(line[i]);
line[i] = null;
}
}
counter = 0;
}
out.close();
System.out.println("Your scrambled document has been written to UnsortedDocument.txt");
}
import java.io.PrintWriter;
import java.io.File;
import java.util.*;
import java.text.*;
public class Sorting{
String[] line = new String[238];
public reSort(){
}
public void readIn()throws Exception {
Scanner stdln = new Scanner(new File(System.getProperty("user.home"),user.home.UnsortedDocument.txt"));
int i = 0;
while (stdln.hasNextLine()){
line[i] = stdln.nextLine();
i++;
}
}
使用PrintWriter时未指定路径。试试这个:
PrintWriter out = new PrintWriter(new File(System.getProperty("user.home"),"user.home.UnsortedDocument.txt"));
非常感谢你。这么简单的事情让我很难堪。再次感谢你。