【File】文件输入/输出。FileInputStream/FileOutputStream类
2017-08-02 20:33
190 查看
一、概述
FileInputStream类和FileOutputStream类都是用来操作磁盘文件。如果文件读取需求比较简单,可以使用FileInputStream(继承InputStream);同理FileOutputStream继承了OutputStream;二、构造方法
FileInputStream(String name);FileInputStream(File file);
三、源代码
下面请看实例(这个例子很经典):package test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; /** * Created by Administrator on 2017/8/2 0002. */ public class FileOutputStreamTest { public static void main(String[] args) { File file = new File("word.txt"); try{ //创建FileOutputStream对象; FileOutputStream out = new FileOutputStream(file);//把file文件打散,拆成流的形式; //创建一个byte数组; byte b[] = "Hello world".getBytes();//把字符串变成字节数组; out.write(b);//流实例对象开始以字节流的方式把字节写入文件,这里注意,我们是站在byte b 的立场来看的,所以是写出,而不是写入; out.close();//关闭流; }catch(Exception e){ e.printStackTrace(); } try{ FileInputStream in= new FileInputStream(file);//创建一个写入流,用来把file中的内容写出来; byte byt [] = new byte[1024];//创建一个byte数组,用来把打散file的流变成byte过滤; int len = in.read(byt);//流的实体通过byte形式来读取file中的内容 System.out.println("文件中的信息是:"+new String(byt,0,len)); in.close();//关闭流 }catch (Exception e){ e.printStackTrace(); } } }
四、截图
这里我们在项目目录下面可以看到,我的空文本word.txt中,写入了新的内容,就是那句“Hello world”;
相关文章推荐
- 黑马程序员-JAVA高级视频_IO输入与输出-20天-6(File文件删除)
- C语言中的文件输入、输出(file I/O)
- 黑马程序员-JAVA高级视频_IO输入与输出-20天-4(File文件写入)
- java 文件的输入和输出 Scanner()与PrintWriter的用法,以及 java.io.file.paths
- 使用文件(FILE)输入输出
- 黑马程序员-JAVA高级视频_IO输入与输出-20天-3(File文件递归)
- java输入输出处理-文件file
- 黑马程序员——第20天——IO输入与输出(File类,Properties,打印流,合并流,切割文件)
- Oracle9i第2版中的UNT_FILE提高了文件输入/输出(I/O)功能。
- 黑马程序员-JAVA高级视频_IO输入与输出-20天-2(File文件列表)
- Ubuntu 16.04使用NASM编译时用ld链接程序出现:i386 架构于输入文件 sandbox.o 与 i386:x86-64 输出不兼容(I386 architecture in the input file sandbox.o is not compatible with i386: x86-64 output)
- 9、流类库与输入/输出2.4二进制输出文件
- 【C语言学习】《C Primer Plus》第13章 文件输入/输出
- C++ 输入输出流和文件输入输出流
- C++ 文件输入和输出fout fin
- python:8:文件的输入与输出
- 文件重定向,getline()获取一样,屏幕输出流,格式控制符dec,oct,hex,精度控制setprecision(int num),设置填充,cout.width和file(字符),进制输入
- C++——输入、输出和文件
- 06年华中科大机试第一题(对输入的5个数排序,输出的结果到文件里)
- [入门-9] IO之文件输入和输出