「java读取文件按行」java 逐行读取文件

博主:adminadmin 2022-12-21 17:24:07 58

今天给各位分享java读取文件按行的知识,其中也会对java 逐行读取文件进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何用java按行读取文本文件

File file = new File("文件地址");

Scanner scanner = new Scanner(file);

String lineContent = null;

while(scanner.hasNextLine()){//如果有下一行

lineContent = scanner.nextLine();//读取下一行内容

}

scanner.close();//关闭Scanner

java怎么读入文件,并逐行输出

java读入文件,并逐行输出,先在D://home建立个文件夹,然后创建一个a.txt文件,然后编辑文件,文本编辑的编码是utf-8,然后用流逐行读取输出,如下:

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

public class TestC {

public static void main(String[] args){

//获取要读取的文件

 File readFile=new File("D://home/a.txt");

 //输入IO流声明

        InputStream in=null;

        InputStreamReader ir=null;

        BufferedReader br=null;

        

        try {

         //用流读取文件

in=new BufferedInputStream(new FileInputStream(readFile));

//如果你文件已utf-8编码的就按这个编码来读取,不然又中文会读取到乱码

ir=new InputStreamReader(in,"utf-8");

//字符输入流中读取文本,这样可以一行一行读取

br= new BufferedReader(ir);

String line="";

//一行一行读取

while((line=br.readLine())!=null){

System.out.println(line);

}

        } catch (Exception e) {

e.printStackTrace();

}finally{

//一定要关闭流,倒序关闭

try {

if(br!=null){

br.close();

}

if(ir!=null){

ir.close();

}

if(in!=null){

in.close();

}

} catch (Exception e2) {

}

}

    

}

}

结果:

helloworld

您好

123456

java读txt方法

1).按行读取TXT文件

package zc;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

public class readLine {

public static void main(String[] args) {

// TODO Auto-generated method stub

File file = new File("C:/zc.txt");

BufferedReader reader = null;

String tempString = null;

int line =1;

try {

System.out.println("以行为单位读取文件内容,一次读一整行:");

reader = new BufferedReader(new FileReader(file));

while ((tempString = reader.readLine()) != null) {

System.out.println("Line"+ line + ":" +tempString);

line ++ ;

}

reader.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

if(reader != null){

try {

reader.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

2).按字节读取TXT文件

package zc;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

public class readerFileByChars {

public static void main(String[] args) {

// TODO Auto-generated method stub

File file = new File("c:/zc.txt");

InputStream in = null;

byte[] tempByte = new byte[1024];

int byteread = 0;

try {

System.out.println("以字节为单位读取文件内容,一次读多个字节:");

in = new FileInputStream(file);

while ((byteread = in.read(tempByte)) != -1 ) {

System.out.write(tempByte, 0, byteread);

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

if (in != null) {

try {

in.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

JAVA编程:读文件,按行输出文件内容

其实你贴的代码并没有问题

不过你可能也发现了,出现了乱码。当然这个乱码不是必然产生的。

这段代码或使用当前环境默认的编码方式去读取test.txt的字符串,如果默认编码与test.txt的编码不一致就可能会导致乱码。

这里附上另一段代码,自定义编码方式

public static void main(String[] args) {

  try {

   // 将D:/test.txt文件读取到输入流中

   InputStream input = new FileInputStream("D:/test.txt");

   // 创建BufferedReader,以gb2312的编码方式读取文件

   BufferedReader reader = new BufferedReader(new InputStreamReader(input, "gb2312"));

   String line = null;

   // 按行读取文本,直到末尾(一般都这么写)

   while ((line = reader.readLine()) != null) {

    // 打印当前行字符串

    System.out.println(line);

   }

  } catch (FileNotFoundException e) {

   e.printStackTrace();

  } catch (IOException e) {

   e.printStackTrace();

  }

 }

关于java读取文件按行和java 逐行读取文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-21,除非注明,否则均为首码项目网原创文章,转载请注明出处。