请问用Java如何逐行的读取一个文本文件呀?我现在只能完整读取.
16lz
2021-01-22
要想逐行读取文本文件里的某一行,应该如何取读取?
谢谢!下面是我现在的代码(我借鉴的是CSDN上别的大侠老师的代码),
很郁闷我只能一次性全部读取.
publicclasstemp{
publicstaticvoidmain(String[]args)throwsIOException{
inti;
FileInputStreamfin;
try{
fin=newFileInputStream("temp.txt");
}catch(FileNotFoundExceptione){
System.out.println(e);
return;
}
intii=0;
byte[]b=newbyte[200];
do{
i=fin.read();
if(i!=-1)b[ii++]=(byte)i;
}while(i!=-1);
fin.close();
System.out.println(newString(b));
}
}
烦请那位高手大侠指点我一下,谢谢先!!!
//ProgramName:IOTest.java
//Author:Tasia
//Time:2003.12.16
importjava.io.*;
publicclassIOTest {
publicstaticvoidmain(String[]args) throwsIOException {
BufferedReaderin=newBufferedReader(
newFileReader("IOTest.java"));
PrintWriterout=newPrintWriter(
newBufferedWriter(
newFileWriter("IOTest.txt")));
Strings1,s=newString();
while((s1=in.readLine())!=null){
s+=s1+"\n";
//S1是每一行的文本,在这里你可以寻S1进行操作。
System.out.println(s1);
out.println(s1);
}
out.close();
System.out.println(s);
}
}
谢谢!下面是我现在的代码(我借鉴的是CSDN上别的大侠老师的代码),
很郁闷我只能一次性全部读取.
publicclasstemp{
publicstaticvoidmain(String[]args)throwsIOException{
inti;
FileInputStreamfin;
try{
fin=newFileInputStream("temp.txt");
}catch(FileNotFoundExceptione){
System.out.println(e);
return;
}
intii=0;
byte[]b=newbyte[200];
do{
i=fin.read();
if(i!=-1)b[ii++]=(byte)i;
}while(i!=-1);
fin.close();
System.out.println(newString(b));
}
}
烦请那位高手大侠指点我一下,谢谢先!!!
3 个解决方案
#1
//ProgramName:IOTest.java
//Author:Tasia
//Time:2003.12.16
importjava.io.*;
publicclassIOTest {
publicstaticvoidmain(String[]args) throwsIOException {
BufferedReaderin=newBufferedReader(
newFileReader("IOTest.java"));
PrintWriterout=newPrintWriter(
newBufferedWriter(
newFileWriter("IOTest.txt")));
Strings1,s=newString();
while((s1=in.readLine())!=null){
s+=s1+"\n";
//S1是每一行的文本,在这里你可以寻S1进行操作。
System.out.println(s1);
out.println(s1);
}
out.close();
System.out.println(s);
}
}
更多相关文章
- 如何判断用户的java代码是否已成功编译?
- 如何获得嵌入式Jetty Web服务器来转储其JSP的临时Java代码
- 急求用jersey2.x+spring3.x 开发rest webservice Java代码实例
- “java.exe”已退出,代码为 1。
- Java 编程下的同步代码块(售票员)
- JAVA 关于图片上传的代码
- 线程“main”中的异常java.lang.RuntimeException:无法编译的源代
- java 和 C 代码运行效率的比较(整理)
- Java:IntelliJ想法生成的代码错误地为所有Class名称添加了其包名