Java文本文件读取

版权声明:本文为博主原创文章,转载请加入原文链接,谢谢。。 https://blog.csdn.net/shawncheer/article/details/86288871

参考: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);
	
	}

猜你喜欢

转载自blog.csdn.net/shawncheer/article/details/86288871