demotextfileio.java

来自「国外的数据结构与算法分析用书」· Java 代码 · 共 32 行

JAVA
32
字号
import java.io.*;
import java.util.*;

/**	Example of a program that writes to a text file, and then
	reads from the file and displays the results on the console. */
public class DemoTextFileIO 
{
	public static void main (String[] args) throws IOException
	{
		PrintWriter myOutFile = new PrintWriter(new FileWriter("myTextFile.txt")); 
		myOutFile.print(7); 
		myOutFile.print("     " + 6 + "\n"); 
		myOutFile.println("Hello World"); 
		myOutFile.print(123.456); 
		myOutFile.close();

		BufferedReader myInFile = new BufferedReader(new FileReader("myTextFile.txt")); 
		String currentLine = myInFile.readLine();
		StringTokenizer wordFinder = new StringTokenizer (currentLine);
		while (wordFinder.hasMoreTokens())
		{
			int a = Integer.parseInt(wordFinder.nextToken());
			System.out.println(a);
		}
		String b = myInFile.readLine();
		currentLine = myInFile.readLine();
		float c = Float.parseFloat(currentLine);
		myInFile.close();
		System.out.println(b + "\n" + c);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?