Java文本文件读取

参考:https://www.geeksforgeeks.org/different-ways-reading-text-file-java/

1.使用BufferedReader

public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		String dirname="C:\\Users\\Administrator\\Desktop\\file_open_test.txt";
		File f1=new File(dirname);
		if (f1.isDirectory()){
			System.out.println(dirname+" is a directory");
		}else{
			BufferedReader br = new BufferedReader(new FileReader(f1));
			String st;
			try {
				while(null != (st = br.readLine()))
					System.out.println(st);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}

2.使用FileReader

public static void main(String[] args){
		// TODO Auto-generated method stub
		String dirname="C:\\Users\\Administrator\\Desktop\\file_open_test.txt";
		
//		// Creates a new FileReader, given the
//		// File to read from.
//		FileReader(File file)
//
//		// Creates a new FileReader, given the
//		// FileDescriptor to read from.
//		FileReader(FileDescriptor fd)
//
//		// Creates a new FileReader, given the
//		// name of the file to read from.
//		FileReader(String fileName)
		
		
		FileReader fr = null;
		try {
			fr = new FileReader(dirname);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int i;
		try {
			while((i=fr.read())!=-1)
				System.out.print((char)i);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}

3.使用Scanner 

	public static void main(String[] args) throws FileNotFoundException{
		// TODO Auto-generated method stub
		String dirname="C:\\Users\\Administrator\\Desktop\\file_open_test.txt";
		File f = new File(dirname);
		Scanner sc = new Scanner(f);
		while(sc.hasNextLine())
			System.out.println(sc.nextLine());
		sc.close();
	
	}

4.使用Scanner class but without using loops

public static void main(String[] args) throws FileNotFoundException{
		// TODO Auto-generated method stub
		String dirname="C:\\Users\\Administrator\\Desktop\\file_open_test.txt";
		File f = new File(dirname);
		Scanner sc = new Scanner(f);
		sc.useDelimiter("\\Z");
		
		System.out.print(sc.nextLine());
		sc.close();
	
	}

5.Reading the whole file in a List

 Read all lines from a file. This method ensures that the file is closed when all bytes have been read or an I/O error, or other runtime exception, is thrown. Bytes from the file are decoded into characters using the specified charset.

public static List readAllLines(Path path,Charset cs)throws IOException

This method recognizes the following as line terminators:

\u000D followed by \u000A, CARRIAGE RETURN followed by LINE FEED
\u000A, LINE FEED
\u000D, CARRIAGE RETURN
// Java program to illustrate reading data from file

// using nio.File

import java.util.*;

import java.nio.charset.StandardCharsets;

import java.nio.file.*;

import java.io.*;

public class ReadFileIntoList

{

  public static List<String> readFileInList(String fileName)

  {

  

    List<String> lines = Collections.emptyList();

    try

    {

      lines =

       Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);

    }

  

    catch (IOException e)

    {

  

      // do something

      e.printStackTrace();

    }

    return lines;

  }

  public static void main(String[] args)

  {

    List l = readFileInList("C:\\Users\\pankaj\\Desktop\\test.java");

  

    Iterator<String> itr = l.iterator();

    while (itr.hasNext())

      System.out.println(itr.next());

  }

}

6.Read a text file as String in Java

public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
		String dirname="C:\\Users\\Administrator\\Desktop\\file_open_test.txt";
		String data = new String(Files.readAllBytes(Paths.get(dirname)));
		System.out.println(data);
	
	}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值