文件字节流FileInputStream是读文件内容

有一下五个方法

1) abstract int read( );

2) int read( byte b[ ] );

3) int read( byte b[ ], int off, int len );

4) int available( );

5) close( );

package com.bjsxt.ioproject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; public class TestInputStream {
public static void main(String[] args) throws IOException {
//1.数据源与应用程序之间搭建管道
FileInputStream fStream=new FileInputStream(new File("F:\\***\\资料\\a.txt"));
//2.从数据源开始向程序中读数据
int count =fStream.available();
System.out.println(count);
//一次读一个字节
// System.out.println( fStream.read());//读取一个字节
// System.out.println(fStream.available());//还有25个字节等待读取
int buf=0;
int count1=0;
while ((buf=fStream.read())!=-1) {
count1++;
System.out.print((char)buf);
}
System.out.println("\n"+count1);
fStream.close();
}
}



使用 FileOutputStream

1) abstract void write( int b );

2) void write( byte b[ ] );

3) void write( byte b[ ], int off, int len );

4) void flush( );

5) void close( );

package com.bjsxt.ioproject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; public class TestOutputStream {
public static void main(String[] args) {
FileOutputStream fOutputStream=null;
try {
fOutputStream = new FileOutputStream(new File("F:\\***\\资料\\a.txt"),true);
byte [] b ="qwerwasd".getBytes();
fOutputStream.write(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (fOutputStream!=null) {
fOutputStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

运行前:

运行后:



补充说明:

fOutputStream = new FileOutputStream(new File("F:\\***\\资料\\a.txt"),true);

true表示是在文件原有的内容之上增加内容。如果不写,则是替换原有内容。覆盖内容。

Java修炼——FileInputStream和FileOutputStream的更多相关文章

  1. java中FileInputStream和FileOutputStream对图片操作的例子

    package a.ab; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.F ...

  2. Java中用FileInputStream和FileOutputStream读写txt文件,文件内容乱码的问题,另附循环代码小错误

    乱码问题大概就是编码格式不一样,搜了很多都是这么说的,修改编码解决乱码问题链接: https://blog.csdn.net/weixin_42496466/article/details/81189 ...

  3. 【Java IO】FileInputStream 和 FileOutputStream

    class FileInputStream extends  InputStream implements Closeable

  4. java中OutputStream字节流与字符流InputStreamReader 每一种基本IO流BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWriter,FileInputStream,FileReader,FileWriter,InputStr

    BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWri ...

  5. java基础知识回顾之javaIO类---FileInputStream和FileOutputStream字节流复制图片

    package com.lp.ecjtu; import java.io.FileInputStream; import java.io.FileNotFoundException; import j ...

  6. java io系列07之 FileInputStream和FileOutputStream

    本章介绍FileInputStream 和 FileOutputStream 转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_07.html File ...

  7. Java之IO(六)FileInputStream和FileOutputStream

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7001458.html 1.前言 前五章按照JDK的类顺序介绍了几种流,第五章讲了Java的文件系统.本章介绍Ja ...

  8. Java IO流的写入和写出操作 FileInputStream和FileOutputStream

    今天学习了Java的IO流,关于文件的读入和写出,主要是FileInputStream和FileOutputStream来实现,这两个流是字节流.还有字符流(FileReader和FileWriter ...

  9. Java IO(五)字节流 FileInputStream 和 FileOutputStream

    Java IO(五)字节流 FileInputStream 和 FileOutputStream 一.介绍 字节流 InputStream 和 OutputStream 是字节输入流和字节输出流的超类 ...

随机推荐

  1. IDEA升级,提示"Connection Error Failed to prepare an update"

    问题来源: 之前修改了IDEA的默认配置文件路径,然后升级新版本时就无法升级,提示"Failed to prepare an update Temp directory inside ins ...

  2. VMware虚拟机添加5个RAID10磁盘并挂载

    1.打开虚拟机之前先添加硬盘设备 具体添加方法详见我https://www.cnblogs.com/Ghost-m/p/11707996.html这个博客 制作RAID需要用到madam命令 mdad ...

  3. .NET Core 3.0 单元测试与 Asp.Net Core 3.0 集成测试

    单元测试与集成测试 测试必要性说明 相信大家在看到单元测试与集成测试这个标题时,会有很多感慨,我们无数次的在实践中提到要做单元测试.集成测试,但是大多数项目都没有做或者仅建了项目文件.这里有客观原因, ...

  4. docker初解

    1 什么是容器 容器就是在隔离的环境中运行的一个进程,如果进程停止,容器就会退出. 隔离的环境拥有自己的系统文件,ip地址,主机名等 容器是一种软件打包技术 程序:代码,命令进程:正在运行的程序容器的 ...

  5. nyoj 455-黑色帽子

    455-黑色帽子 内存限制:64MB 时间限制:1000ms 特判: No 通过数:4 提交数:7 难度:1 题目描述:         最近发现了一个搞笑的游戏,不过目前还没玩过.一个舞会上,每个人 ...

  6. Win32窗口消息机制 x Android消息机制 x 异步执行

    如果你开发过Win32窗口程序,那么当你看到android代码到处都有的mHandler.sendEmptyMessage和 private final Handler mHandler = new ...

  7. 使用OpenCV和imagezmq通过网络实时传输视频流 | live video streaming over network with opencv and imagezmq

    本文首发于个人博客https://kezunlin.me/post/b8847d9f/,欢迎阅读最新内容! live video streaming over network with opencv ...

  8. Ubuntu 16.04安装ROS Kinetic详细教程 | Tutorial to Install and Configure ROS Kinetic on Ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/e2780b93/,欢迎阅读! Tutorial to Install and Configure ROS Kinetic on U ...

  9. 编译k8s1.11 kube-apiserver源码

    说明 修改k8s1.11 kube-apiserver源码并构建成镜像,最终替换运行在集群中的kube-apiserver pod来验证本次源码修改 参看链接:https://xinchen.blog ...

  10. 20191010-5 alpha week 1/2 Scrum立会报告+燃尽图 03

    此作业要求参见[https://edu.cnblogs.com/campus/nenu/2019fall/homework/8748] 一.小组情况 队名:扛把子 组长:迟俊文 组员:宋晓丽 梁梦瑶 ...