输入一个目录到 Java 以继续
input a directory to Java to be proceed
我这里有问题。每当我输入从我的计算机复制的目录路径以向我的程序输入一个txt文件时,它总是说找不到该文件。我的代码有问题吗?
System.out.println("insert directory file = ");
FileReader file = null;
try {
file = new FileReader(input.next());
BufferedReader readfile = new BufferedReader(file);
StringBuffer sb = new StringBuffer();
try {
while ((text = readfile.readLine()) != null) {
sb.append(text);
sb.append("\n");
}
readfile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text = sb.toString();
//System.out.println(text);
System.out.println("Data entered");
System.out.println("Data length = "+text.length()+"\n");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
System.out.println("File not found. Pease insert the proper file directory.\n");
}
您的 代码段 运行 在我的笔记本电脑上没问题。所以问题可能在这里:
file = new FileReader(input.next());
在读取路径之前,您是否使用扫描仪进行其他输入?尝试将其更改为
String path = input.next();
file = new FileReader(path);
并在发生错误时打印路径以查看实际传递给您的 FileReader 的内容。
catch (FileNotFoundException e1) {
System.out.println("File not found. Pease insert the proper file directory.\n");
System.out.println("Your input path: " + path);
}
这是我机器上的工作代码:
public static void main(String[] args) {
String path = null;
try (Scanner input = new Scanner(System.in)) {
System.out.print("Input your option = ");
int option = input.nextInt();
switch (option) {
case 1:
System.out.println("insert directory file = ");
String text = "";
path = input.next();
FileReader fileReader = new FileReader(path);
BufferedReader readfile = new BufferedReader(fileReader);
StringBuffer sb = new StringBuffer();
try {
while ((text = readfile.readLine()) != null) {
sb.append(text);
sb.append("\n");
}
readfile.close();
} catch (IOException e) {
e.printStackTrace();
}
text = sb.toString();
System.out.println("Data entered");
System.out.println("Data length = " + text.length() + "\n");
break;
default:
System.out.println("There is nothing to do.");
break;
}
} catch (FileNotFoundException e1) {
System.out.println("File not found. Pease insert the proper file directory.");
System.out.println("Your input path is : " + path);
}
}
我这里有问题。每当我输入从我的计算机复制的目录路径以向我的程序输入一个txt文件时,它总是说找不到该文件。我的代码有问题吗?
System.out.println("insert directory file = ");
FileReader file = null;
try {
file = new FileReader(input.next());
BufferedReader readfile = new BufferedReader(file);
StringBuffer sb = new StringBuffer();
try {
while ((text = readfile.readLine()) != null) {
sb.append(text);
sb.append("\n");
}
readfile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text = sb.toString();
//System.out.println(text);
System.out.println("Data entered");
System.out.println("Data length = "+text.length()+"\n");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
System.out.println("File not found. Pease insert the proper file directory.\n");
}
您的 代码段 运行 在我的笔记本电脑上没问题。所以问题可能在这里:
file = new FileReader(input.next());
在读取路径之前,您是否使用扫描仪进行其他输入?尝试将其更改为
String path = input.next();
file = new FileReader(path);
并在发生错误时打印路径以查看实际传递给您的 FileReader 的内容。
catch (FileNotFoundException e1) {
System.out.println("File not found. Pease insert the proper file directory.\n");
System.out.println("Your input path: " + path);
}
这是我机器上的工作代码:
public static void main(String[] args) {
String path = null;
try (Scanner input = new Scanner(System.in)) {
System.out.print("Input your option = ");
int option = input.nextInt();
switch (option) {
case 1:
System.out.println("insert directory file = ");
String text = "";
path = input.next();
FileReader fileReader = new FileReader(path);
BufferedReader readfile = new BufferedReader(fileReader);
StringBuffer sb = new StringBuffer();
try {
while ((text = readfile.readLine()) != null) {
sb.append(text);
sb.append("\n");
}
readfile.close();
} catch (IOException e) {
e.printStackTrace();
}
text = sb.toString();
System.out.println("Data entered");
System.out.println("Data length = " + text.length() + "\n");
break;
default:
System.out.println("There is nothing to do.");
break;
}
} catch (FileNotFoundException e1) {
System.out.println("File not found. Pease insert the proper file directory.");
System.out.println("Your input path is : " + path);
}
}