package shi;
import java.io.*;
public class shi {
static final String FileName="d:/shirong/lovejava.txt";//定义文件的路径
public static void main(String[] args) throws IOException{
// TODO 自动生成的方法存根
String str;
InputStreamReader instr=new InputStreamReader(System.in);//定义输入文件类InputStreamReader
BufferedReader bufred=new BufferedReader(instr); //定义读入缓冲器BufferedReader
OutputStreamWriter outstr=new OutputStreamWriter(System.out);//定义输出文件类OutputStreamWriter
BufferedWriter bufwt=new BufferedWriter(outstr); //定义写入缓冲器BufferedWriter
RandomAccessFile fisIn=new RandomAccessFile(FileName,"rw");//定义随机读取类RandomAccessFile
try{
System.out.print("请输入内容(按<Enter>键结束):\n");
str=bufred.readLine(); //读入流对象通过调用方法readLine()实现按行读取信息
System.out.print("在路径"+FileName+"中存储内容...\n");
fisIn.writeBytes(str); //将读取的内容写入文件流对象fisIn中
System.out.print("输入内容:\n");
bufwt.write(str); //将信息强制输出
bufwt.write((int)('\n'));
bufwt.flush();
}
catch(IOException ex){
System.out.println("输入/输出异常!");
}
}
}
文件的写入
最新推荐文章于 2023-12-07 15:25:55 发布