英文:
only printing the string present on the first line of the text in java
问题
我是Java新手,想逐字符打印文本文件中的字符串,但它只打印第一行的字符串,当我在文本文件的下一行写些东西时,代码不打印它们。有人能帮助我吗?我附上了以下代码!
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class main {
public static void main(String[] args) throws FileNotFoundException {
char ch;
File newFile = new File("C:/temp/sourcecode.txt");
Scanner scanFile = new Scanner(newFile);
String str;
str = scanFile.nextLine();
int l = str.length();
for(int i =0; i<l ; i++) {
ch = str.charAt(i);
System.out.println(ch);
}
}
}
提前谢谢!
英文:
I am new to java and want to print the string present in the text file character by character but its only printing the first line string and when I write something on the next line in the text file the code is not printing them. Can someone help me in this. I am attaching the code below!
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class main {
public static void main(String[] args) throws FileNotFoundException {
char ch;
File newFile = new File("C:/temp/sourcecode.txt");
Scanner scanFile = new Scanner(newFile);
String str;
str = scanFile.nextLine();
int l = str.length();
for(int i =0; i<l ; i++) {
ch = str.charAt(i);
System.out.println(ch);
}
}
}
Thank you in advance!
答案1
得分: 1
以下是您要翻译的内容:
我是Java的新手,想逐字符打印文本文件中的字符串,但只打印第一行字符串。
这是因为您只读取了第一行。此外,您在读取行时没有检查文件中是否有任何行,因此如果文件为空,您将会收到异常。您必须始终在调用scanFile.nextLine()
读取文件的一行之前检查scanFile.hasNextLine()
是否为真。
为了对每一行重复进行相同的处理,您需要使用循环,而对于这个需求来说,最自然的循环是while
循环。所以,您只需要将以下代码:
String str;
str = scanFile.nextLine();
int l = str.length();
for (int i = 0; i < l; i++) {
ch = str.charAt(i);
System.out.println(ch);
}
放入一个while
循环块中,如下所示:
while (scanFile.hasNextLine()) {
String str;
str = scanFile.nextLine();
int l = str.length();
for (int i = 0; i < l; i++) {
ch = str.charAt(i);
System.out.println(ch);
}
}
英文:
> I am new to java and want to print the string present in the text file
> character by character but its only printing the first line string
It's because you are reading only the first line. Moreover, you are reading the line without checking if there is any line in the file or not and therefore you will get an exception if your file is empty. You must always check, if scanFile.hasNextLine()
before calling scanFile.nextLine()
to read a line from the file.
In order to repeat the process for each line, you need a loop and most natural loop for this requirement will be while
loop. So, all you need to do is to put the following code:
String str;
str = scanFile.nextLine();
int l = str.length();
for (int i = 0; i < l; i++) {
ch = str.charAt(i);
System.out.println(ch);
}
into a while
loop block as shown below:
while (scanFile.hasNextLine()) {
String str;
str = scanFile.nextLine();
int l = str.length();
for (int i = 0; i < l; i++) {
ch = str.charAt(i);
System.out.println(ch);
}
}
答案2
得分: 0
你需要与扫描器一起工作(而不是从nextLine()返回的字符串)。如果仔细查看,您只读取了文件一次。您的循环应该类似于这样:
while(scanFile.hasNextLine()){
String line = scanFile.nextLine();
}
关于hasNextLine的JavaDoc API
英文:
You will want to work with the scanner (not the string retuned from nextLine(). If you look carefully you are reading your file once. Your loop should look something like this:
while(scanFile.hasNextLine()){
String line = scanFile.nextLine();
}
JavaDoc API on hasNextLine
答案3
得分: 0
因为 nextLine()
会读取文件直到遇到 Scanner.LINE_PATTERN
(在你的情况下是 \r\n
)。要读取整个文件可以使用以下代码:
while (scanFile.hasNextLine()){
str = scanFile.nextLine();
// 在这里添加你的代码
}
英文:
It happens because nextLine()
reads a file until Scanner.LINE_PATTERN
(\r\n
in your case).
To read the whole file:
while (scanFile.hasNextLine()){
str = scanFile.nextLine();
//your code here
}
答案4
得分: 0
尝试一下。
File file = new File("/my/location");
String contents = new Scanner(file).useDelimiter("\\Z").next();
学有所乐
英文:
Try this.
File file = new File("/my/location");
String contents = new Scanner(file).useDelimiter("\\Z").next();
Happy learning
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论