java 写入文件(FileOutputStream的使用)

来源:互联网 发布:openwrt破解网络尖兵 编辑:程序博客网 时间:2024/06/04 19:16
实现在控制台写入相关文字,并添加到指定的文件中,并实现读取文件内容import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.Scanner;public class Fileout {Scanner inScanner = new Scanner(System.in);public void f(String address) throws IOException{FileOutputStream out=new FileOutputStream(address, true);String buf;System.out.println("请输入");buf= inScanner.nextLine();byte buy[]=buf.getBytes();out.write(buy);FileInputStream fin=new FileInputStream(address);byte[] bs=new byte[1024];int count=0;while((count=fin.read(bs))>0){String str=new String(bs,0,count);//反复定义新变量:每一次都 重新定义新变量,接收新读取的数据System.out.println(str);//反复输出新变量:每一次都 输出重新定义的新变量}fin.close();}public static void main(String[] args) throws IOException {Fileout out=new Fileout();out.f("f://student.txt");}}


阅读全文
0 0
原创粉丝点击