gpt4 book ai didi

java - 从我的桌面读取文件并打印其中的每一行

转载 作者:行者123 更新时间:2023-12-01 12:52:25 25 4
gpt4 key购买 nike

亲爱的大家,我有这个代码:

File file = new File("flowers_petal.txt");
Scanner in = new Scanner(file);
while(in.hasNext()){
String line = in.nextLine();
System.out.println(line);

我想从文件中读取并打印每一行,但是由于一些异常(抛出异常??),此代码不起作用,我怎样才能以从flowers.txt中读取的方式放置它文件,它位于我的桌面上,并将在控制台中打印该文件中的每一行?

最佳答案

重新检查您的代码

File file = new File("flowers_petal.txt"); // This is not your desktop location.. You are probably getting FileNotFoundException. Put Absolute path of the file here..
while(in.hasNext()){ // checking if a "space" delimited String exists in the file
String line = in.nextLine(); // reading an entire line (of space delimited Strings)
System.out.println(line);

旁注:使用 FileReader + BufferedReader 来“读取”文件。使用扫描仪解析文件..

关于java - 从我的桌面读取文件并打印其中的每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24118719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com