java_文件输入与输出

1. 从文件中读取信息

//从文件中读取数据信息并打印输出
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Scanner; //三个包不能少

public class Test {
	public static void main(String args[]) throws IOException //这个throws不能少
	{
		Scanner in = new Scanner(Paths.get("d:\\1.txt"));  
		String data = in.nextLine();
		String data2 = in.nextLine();
		System.out.println(data + '\n' + data2);
	}
}

2. 写入数据信息到文件中

//向文件中写入数据信息
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner; //三个包不能少

public class Test {
	public static void main(String args[]) throws IOException //这个throws不能少
	{
		PrintWriter out = new PrintWriter("d:\\2.txt");
		out.println("hahahaha");  //写入数据信息到指定文件
		out.flush(); //刷新文件
	}
}
发布了137 篇原创文章 · 获赞 51 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/Pop_Rain/article/details/73432527
今日推荐