Java 如何读取.txt文件中的下一行
我可以知道为什么上传时无法读取.txt文件的下一行吗。 我尝试上载的所有.txt文件都会发生这种情况,然后使用system.out.println()上载。 在我的文本文件中,它包含:猫、狗、猴子(每一行)。。但输出的值是:Java 如何读取.txt文件中的下一行,java,amazon-s3,amazon,Java,Amazon S3,Amazon,我可以知道为什么上传时无法读取.txt文件的下一行吗。 我尝试上载的所有.txt文件都会发生这种情况,然后使用system.out.println()上载。 在我的文本文件中,它包含:猫、狗、猴子(每一行)。。但输出的值是: [Monkey][Monkey, null][Monkey, null, null][Monkey, null, null, null][Monkey, null, null, null, null][Monkey, null, null, null, null, null
[Monkey][Monkey, null][Monkey, null, null][Monkey, null, null, null][Monkey, null, null, null, null][Monkey, null, null, null, null, null][Monkey, null, null, null, null, null, null]
在这方面需要帮助。
多谢各位
想知道为什么我能读.txt文件而不能读.doc。我们也需要这方面的建议
import java.awt.List;
import java.io.*;
import java.util.ArrayList;
import javax.imageio.IIOException;
import javax.swing.JFileChooser;
public class searchforCapitalLetter {
public static void main(String[] args) throws IOException{
try {
// file chooser
JFileChooser chooser=new JFileChooser();
int returnVal = chooser.showOpenDialog(null);{
if(returnVal == JFileChooser.APPROVE_OPTION)
{ File f = chooser.getSelectedFile();}
FileInputStream fin = new FileInputStream(chooser.getSelectedFile());
DataInputStream din = new DataInputStream(fin);
BufferedReader br = new BufferedReader(new InputStreamReader(din));
ArrayList<String>list =new ArrayList<String> ();
if ((br.readLine()) != null) {
while (br.readLine() != " ") {
list.add(br.readLine());
System.out.print (list);
} br.close() ;
}//closes if statement
} // closes method dialog
} // closes try method
catch (FileNotFoundException e) {
e.printStackTrace();
} // closes catch method
} // closes method body
} // closes class method
导入java.awt.List;
导入java.io.*;
导入java.util.ArrayList;
导入javax.imageio.IIOException;
导入javax.swing.JFileChooser;
公共类searchforCapitalLetter{
公共静态void main(字符串[]args)引发IOException{
试试{
//文件选择器
JFileChooser chooser=新的JFileChooser();
int returnVal=chooser.showOpenDialog(null){
if(returnVal==JFileChooser.APPROVE_选项)
{File f=chooser.getSelectedFile();}
FileInputStream fin=新的FileInputStream(chooser.getSelectedFile());
DataInputStream din=新的DataInputStream(fin);
BufferedReader br=新的BufferedReader(新的InputStreamReader(din));
ArrayList=新的ArrayList();
如果((br.readLine())!=null){
而(br.readLine()!=“”){
list.add(br.readLine());
系统输出打印(列表);
}br.close();
}//关闭if语句
}//关闭方法对话框
}//关闭try方法
catch(filenotfounde异常){
e、 printStackTrace();
}//关闭捕获方法
}//关闭方法体
}//关闭类方法
您不能使用.doc的原因是.doc文件是格式化的,因此,有些东西您的代码需要解析才能从中读取
就奇怪的打印而言,在打印代码之前,您已经阅读了两次代码(每次调用.readLine时,它都会将扫描仪移动到下一行)。请尝试以下代码:
ArrayList<String>list =new ArrayList<String> ();
String currentLine;
while ((currentLine = br.readLine()) != null) {
list.add(currentLine);
System.out.println(currentLine);
} br.close() ;
}
ArrayListlist=newarraylist();
串电流线;
而((currentLine=br.readLine())!=null){
列表。添加(当前行);
系统输出打印项次(当前行);
}br.close();
}
这将在变量中跟踪当前行,而不是重复将扫描仪移动到下一行。另一个问题是,当到达文件末尾时,它将为null,而不是“”。无法使用.doc的原因是.doc文件已格式化,因此,代码必须解析某些内容才能从中读取 就奇怪的打印而言,在打印代码之前,您已经阅读了两次代码(每次调用.readLine时,它都会将扫描仪移动到下一行)。请尝试以下代码:
ArrayList<String>list =new ArrayList<String> ();
String currentLine;
while ((currentLine = br.readLine()) != null) {
list.add(currentLine);
System.out.println(currentLine);
} br.close() ;
}
ArrayListlist=newarraylist();
串电流线;
而((currentLine=br.readLine())!=null){
列表。添加(当前行);
系统输出打印项次(当前行);
}br.close();
}
这将在变量中跟踪当前行,而不是重复将扫描仪移动到下一行。另一个问题是,当根据bufferedreader api到达文件末尾时,它将为null,而不是“” 公共字符串读取行() 抛出IOException 读一行文字。一行被认为是由一个换行符('\n')、一个 回车符('\r'),或紧接着 换行 返回:包含行内容的字符串,不包括 任何行终止字符,如果流的结尾有 联系到 抛出:IOException-如果发生I/O错误 请注意,您的代码出现了一些错误: 1) 它调用readline()一次检查返回值,然后再次使用返回值。第一次调用(在while条件下)从缓冲区中删除当前值,因此每隔一行就删除一次 2) 您使用了错误的比较来查看是否有剩余数据。x!=y、 如果x和y都是对象,则检查指针是否相等-分配x的内存位置是否与分配y的内存位置相同。您真正想要的是将值存储在变量中 例如:
BufferedReader in = ...;
String s = in.readLine();
while(s!=null){
...
s = in.readLine();
}
根据bufferedreader api: 公共字符串读取行() 抛出IOException 读一行文字。一行被认为是由一个换行符('\n')、一个 回车符('\r'),或紧接着 换行 返回:包含行内容的字符串,不包括 任何行终止字符,如果流的结尾有 联系到 抛出:IOException-如果发生I/O错误 请注意,您的代码出现了一些错误: 1) 它调用readline()一次检查返回值,然后再次使用返回值。第一次调用(在while条件下)从缓冲区中删除当前值,因此每隔一行就删除一次 2) 您使用了错误的比较来查看是否有剩余数据。x!=y、 如果x和y都是对象,则检查指针是否相等-分配x的内存位置是否与分配y的内存位置相同。您真正想要的是将值存储在变量中 例如:
BufferedReader in = ...;
String s = in.readLine();
while(s!=null){
...
s = in.readLine();
}
从文件中读取内容的经典BufferedReader。 这是我们要用Java读取的文件: 这是要写入文件的内容
package com.stackoverflow;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile{
private static final String FILENAME = "D:\\test\\filename.txt";
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
这是要写入文件的内容
package com.stackoverflow;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile{
private static final String FILENAME = "D:\\test\\filename.txt";
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
输出:
这是要写入文件的内容
package com.stackoverflow;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile{
private static final String FILENAME = "D:\\test\\filename.txt";
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
这是要写入文件的内容从文件读取内容的经典BufferedReader。
package com.stackoverflow;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile{
private static final String FILENAME = "D:\\test\\filename.txt";
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
这是我们要用Java读取的文件:
这是要写入文件的内容
package com.stackoverflow;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile{
private static final String FILENAME = "D:\\test\\filename.txt";
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
这是要写入文件的内容<