gpt4 book ai didi

java - 如何从文件中读取信息并将其存储为字符串。 java

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

我已经走到这一步了,但这无法读取文件,这就是我坚持的部分。我知道您需要使用扫描仪,但我不确定我在这里缺少什么。我认为它还需要文件的路径,但我不知道将其放在哪里

public class string
{

public static String getInput(Scanner in) throws IOException
{



{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter file");
String filename =keyboard.next();

File inputFile = new File(filename);
Scanner input = new Scanner(inputFile);
String line;

while (input.hasNext())
{
line= input.nextLine();
System.out.println(line);
}
input.close();
}
if(filename.isEmpty())
{
System.out.println("Sorry, there has been an error. You must enter a string! (A string is some characters put together.) Try Again Below.");
return getInput(in);
}
else
{
return filename;
}
}



public static int getWordCount(String input)
{

String[] result = input.split(" ");
return result.length;
}




public static void main(String[] args)
{
DecimalFormat formatter = new DecimalFormat("0.##");
String input = getInput(new Scanner(System.in));
float counter = getWordCount(input);
System.out.println("The number of words in this string ("+input+") are: " + counter);
Scanner keyboard= new Scanner(System.in);

}

}
//end of code

最佳答案

首先,在Java中进行文件I/O时,应该正确处理可能发生的所有异常和错误。

一般来说,您需要在try中打开流和资源。 block ,捕获 catch 中发生的所有异常阻止然后关闭 finally 中的所有资源堵塞。您应该阅读更多关于这些 here 的内容。也是如此。

对于使用 Scanner 对象,这看起来像:

    String token = null;
File file = null;
Scanner in = null;
try {
file = new File("/path/to/file.txt");
in = new Scanner(file);
while(in.hasNext()) {
token = in.next();
// ...
}
} catch (FileNotFoundException e) {
// if File with that pathname doesn't exist
e.printStackTrace();
} finally {
if(in != null) { // pay attention to NullPointerException possibility here
in.close();
}
}

您还可以使用 BufferedReader 逐行读取文件。

BufferedReader reader = new BufferedReader(new FileReader("/path/to/file.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
// ...
}

添加了异常处理:

    String line = null;
FileReader fReader = null;
BufferedReader bReader = null;
try {
fReader = new FileReader("/path/to/file.txt");
bReader = new BufferedReader(fReader);
while ((line = bReader.readLine()) != null) {
// ...
}
} catch (FileNotFoundException e) {
// Missing file for the FileReader
e.printStackTrace();
} catch (IOException e) {
// I/O Exception for the BufferedReader
e.printStackTrace();
} finally {
if(fReader != null) { // pay attention to NullPointerException possibility here
try {
fReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bReader != null) { // pay attention to NullPointerException possibility here
try {
bReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

一般来说,解析文件使用Scanner,逐行读取文件使用BufferedReader

还有其他更高级的方法可以在 Java 中执行读/写操作。查看其中一些 here

关于java - 如何从文件中读取信息并将其存储为字符串。 java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25854729/

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