我想读取文本并将其存储在String变量中... 这段代码工作正常,但我想知道txt文件应该放在哪里,我没有指定确切的路径。因为我将在各种机器上尝试此代码。所以我希望你能理解我想要的东西。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Compress {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String text="";
try {
text = new Scanner( new File("C:\\Users\\sandhya\\workspace\\PrefixFreeCodeChecker\\src\\poem.txt"), "UTF-8" ).useDelimiter("\\A").next();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(text+"");
}
}
答案 0 :(得分:0)
将poem.txt文件放在java项目文件夹中,并将路径更改为:
text = new Scanner( new File("poem.txt").....