英文:
Detect the end of line in file
问题
以下是翻译好的部分:
我正在尝试读取一个 txt
文件并显示文件元素。我希望将元素显示在同一行,以实现单行换行。但是,在文件中的新行开始时,要多给一个新行。txt
文件内容如下:
《星际穿越》 % 克里斯托弗·诺兰 % 2014 % PG-13
《盗梦空间》 % 克里斯托弗·诺兰 % 2010 % PG-13
《复仇者联盟:终局之战》 % 卢素斯兄弟 % 2019 % PG-13
这里,%
是分隔符。我目前的做法是:
File file_path = new File("film.txt");
try {
Scanner file_input = new Scanner(file_path);
file_input.useDelimiter("%");
for(new_book=1; file_input.hasNext(); new_book++) {
String book_item = file_input.next().trim();
System.out.println(book_item);
if(new_book%4==0){
System.out.println();
}
}
file_input.close();
}
catch (FileNotFoundException e) {
System.out.println("文件未找到");
}
它给出的结果是:
星际穿越
克里斯托弗·诺兰
2014
PG-13
盗梦空间
克里斯托弗·诺兰
2010
PG-13
复仇者联盟:终局之战
卢素斯兄弟
2019
PG-13
我希望得到的结果是:
星际穿越
克里斯托弗·诺兰
2014
PG-13
盗梦空间
克里斯托弗·诺兰
2010
PG-13
我注意到的问题是,在行末尾索引不起作用,即如果我打印 new_book
,则 new_book
的值不会显示在第一个和第二个 PG-13
中,但会显示在最后一个 PG-13
中。
我对文件中的这一行感到困惑。非常欢迎各种建议!请注意,我不困惑于语法错误,而是困惑于逻辑和文件阅读过程的处理方式。
英文:
I am trying to read a txt
file and display the file elements. I want to display elements in same line to give a single line break. But, give extra new line when new line in file starts. The txt
file is:
> Interstellar % Christopher Nolan % 2014 % PG-13
>
> Inception % Christopher Nolan % 2010 % PG-13
>
> Endgame % Russo Brothers % 2019 % PG-13
Here, %
is delimeter. What I have done so far:
File file_path = new File("film.txt");
try {
Scanner file_input = new Scanner(file_path);
file_input.useDelimiter("%");
for(new_book=1; file_input.hasNext(); new_book++) {
String book_item = file_input.next().trim();
System.out.println(book_item);
if(new_book%4==0){
System.out.println();
}
}
file_input.close();
}
catch (FileNotFoundException e) {
System.out.println("File not found");
}
What it gives:
Interstellar
Christopher Nolan
2014
PG-13
Inception
Christopher Nolan
2010
PG-13
Endgame
Russo Brothers
2019
PG-13
What I want is:
Interstellar
Christopher Nolan
2014
PG-13
Inception
Christopher Nolan
2010
PG-13
What I have noticed is, at the end of line indexing is not working i.e, if I print new_book
then, new_book
value is not shown in first and second PG-13
, but shows in last PG-13
.
I am confused with this line in file. Every suggestion is appreciated!!! Also, note that I am not confuse with syntactical error. But, with the logic and how file reading process is done.
答案1
得分: 1
file_input = new Scanner(file);
while(file_input.hasNextLine()){
String st = file_input.nextLine();
String[] s = st.split("%");
if(s.length > 1)
System.out.println(s[0] + "\n" + s[1] + "\n" + s[2] + "\n" + s[3] + "\n");
}
file_input.close();
英文:
You can try to read the line and then split it
file_input = new Scanner(file);
while(file_input.hasNextLine()){
String st = file_input.nextLine();
String[] s = st.split("%");
if(s.length > 1)
System.out.println(s[0] + "\n" + s[1] + "\n" + s[2] + "\n" + s[3] + "\n");
}
file_input.close();
答案2
得分: 1
我使用了scanner.nextLine()函数一次性读取整行。然后,我使用String.split("%")将行字符串拆分为字符串列表。最后,我在输出之前对每个字符串使用了String.trim()以去除前导和尾随空白。
File file_path = new File("film.txt");
try {
Scanner file_input = new Scanner(file_path);
while(file_input.hasNextLine()){
String line = file_input.nextLine();
String[] items = line.split("%");
for(String item: items)
System.out.println(item.trim());
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
英文:
I used the scanner.nextLine() function to read the entire line at once. Then I split the line string into a list of strings with String.split("%"). Finally I used String.trim() on each String before outputting to remove leading and trailling whitespace.
File file_path = new File("film.txt");
try {
Scanner file_input = new Scanner(file_path);
while(file_input.hasNextLine()){
String line = file_input.nextLine();
String[] items = line.split("%");
for(String item: items)
System.out.println(item.trim());
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
答案3
得分: 1
问题在于换行符号没有被视为分隔符,因此第四个标记被读取为 PG-13<NEWLINE>Inception
- 内部嵌套了一个换行符。你有以下选项:
将换行符添加为可能的分隔符:
Scanner file_input = new Scanner(file_path);
file_input.useDelimiter("%|\n");
或者在输入文件的每一行末尾添加 %
:
> Interstellar % Christopher Nolan % 2014 % PG-13 %<br>
> Inception % Christopher Nolan % 2010 % PG-13 %
或者改变读取文件的方式,逐行读取并按 %
进行分割(参见其他人的回答)。
英文:
The problem is that the line break is not considered as a delimiter so the 4th token is read as PG-13<NEWLINE>Inception
- with a line break embedded inside. Your options are:
Add line break as a possible delimiter:
Scanner file_input = new Scanner(file_path);
file_input.useDelimiter("%|\n");
Or add % at the end of every line in the input file:
> Interstellar % Christopher Nolan % 2014 % PG-13 %<br>
> Inception % Christopher Nolan % 2010 % PG-13 %
Or change the way the file is read to reading line by line and splitting on %
(see answers by others)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论