如何从文件中读入信息,并将其存储为字符串。JAVA
我已经走了这么远,但是在文件中读不到,这是我一直坚持的部分。我知道你需要使用扫描仪,但我不确定我在这里遗漏了什么。我想它也需要一个文件路径,但我不知道该放在哪里如何从文件中读入信息,并将其存储为字符串。JAVA,java,string,file,Java,String,File,我已经走了这么远,但是在文件中读不到,这是我一直坚持的部分。我知道你需要使用扫描仪,但我不确定我在这里遗漏了什么。我想它也需要一个文件路径,但我不知道该放在哪里 public class string { public static String getInput(Scanner in) throws IOException { { Scanner keyboard = new Scanner(System.in); System.out.println("Enter file");
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时,应该正确处理可能发生的所有异常和错误 通常,您需要打开块中的流和资源,捕获块中发生的所有异常,然后关闭块中的所有资源。你也应该读更多关于这些的书 对于使用
扫描器
对象,这将类似于:
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();
}
}
}
通常,使用扫描仪
解析文件,使用缓冲读取器
逐行读取文件
BufferedReader reader = new BufferedReader(new FileReader("/path/to/file.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
// ...
}
还有其他更高级的方法可以在Java中执行读/写操作。查看其中一些请格式化您的代码。如果你没有花时间提出问题,人们就不太可能阅读并回答你的问题。