java基础:12.1 文本I/O(一)

1、java.io.file类

在Windows 中目录的分隔符是反斜杠()。但是在Java 中,反斜杠是一个特殊的字符,应该写成\ 的形式

斜杠(/) 是Java的目录分隔符

构建一个File 实例并不会在机器上创建一个文件。不管文件是否存在,都可以创建任意文件名的File 实例。可以调用File 实例上的exists()方法来判断这个文件是否存在。

public class FileStudy {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		java.io.File file = new java.io.File("FileStudy.java");
		System.out.println("Does it exist? " + file.exists());
		System.out.println("the file has "+file.length() + " bytes");
	}
}

使用Scanner 类从文件中读取文本數据,使用PrintWriter 类向文本文件写入数据。

File 对象封装了文件或路径的属性,但是它既不包括创建文件的方法,也不包括从/ 向文件读/ 写数据(称为数据输入输出,简称I/O) 的方法。为了完成I/O 操作,需要使用恰当的Java I/O 类创建对象。这些对象包含从/ 向文件读/ 写数据的方法。文本文件本质上是存储在磁盘上的字符.

2、 java.io.printWriter

public class WriteData {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		java.io.File file = new java.io.File("scores.txt");
		if(file.exists()) {
			System.out.println("file already exists");
			System.exit(0);
		}
		
		java.io.PrintWriter output = new java.io.PrintWriter(file);
		output.print("jokn");
		output.println(90);
		output.close();
	}
}

上面这种方式,程序员很有可能会忘记关闭文件。所以我们引入了下一种:自动关闭

try( 声明和创建资){
使用资源来处理文件;
}

资源的声明和创建必须在同一行语句中中,,可以在括号中进行多个资源的声明和创建。

public class WriteData_withAutoClose {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		java.io.File file = new java.io.File("scores2.txt");
		if(file.exists()) {
			System.out.println("file already exists");
			System.exit(0);
		}
		
		try(
				java.io.PrintWriter output = new java.io.PrintWriter(file); 
		){
		output.print("jokn");
		output.println(90);
		output.close();
		}
	}

}

在这个java工程的根目录下,生成了我们创建的txt文件

3、java.util.Scanner

java.util.Scanner 类用来从控制台读取字符串和基本类型数值

程序ReadData.java,从scores.txt读取数据

public class ReadData {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub

		java.io.File file = new java.io.File("scores.txt");
		Scanner input = new Scanner(file);
		while (input.hasNext()) {
			String firstName = input.next();
			String mi = input.next();
			String lastName = input.next();
			int score = input.nextInt();
			System.out.println(firstName + "  " + mi + "  " +lastName+" "+score);
		}
		input.close();
	}

}

可以使用useDelimiter(String regex)方法设置新的分隔符模式

注意:

方法next()和nextLine()都会读取一个字符串。

next()方法读取一个由分隔符分隔的字符串,但是nextLine()读取一个以换行符结束的行。

标记读取方法不能读取标记后面的分隔符。如果在标记读取方法之后调用nextLine(),该方法读取从这个分隔符开始,到这行的行分隔符结束的字符。这个行分隔符也被读取,但是它不是nextLine() 返回的字符串部分。

示例学习:

java ReplaceText ForatString.java t.txt StringBuilder StringBuffer

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class ReplaceText {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		//用一个新字符串替换文本文件中所有出现某个字符串的地方
		if(args.length!=4) {
			System.out.println("Usage:java ReplaceText sourceFile targetFile oldStr newStr");
			System.exit(1);
		}
		
		//check if source file exists
		File sourceFile = new File(args[0]);
		if(!sourceFile.exists()) {
			System.out.println("source file " + args[0] + " does not exist");
			System.exit(2);
		}
		
		//check if target file exists
		File targetFile = new File(args[1]);
		if(!sourceFile.exists()) {
			System.out.println("target file " + args[1] + " does not exist");
			System.exit(3);
		}
		
		try(
				Scanner input = new Scanner(sourceFile);
				PrintWriter output = new PrintWriter(targetFile);
			){
			while (input.hasNext()) {
				String s1=input.nextLine();
				String s2=s1.replaceAll(args[2], args[3]);
				output.println(s2);
			}
		}
	}

}
原文地址:https://www.cnblogs.com/l20902/p/10610937.html