文件路径:

public static final String FILEPATH= File.separator+"Users"+ File.separator+"xuminzhe"+
File.separator+"Documents"+File.separator+"io";

1.创建文件


public static void main(String[] args) {
File file=new File(Constant.FILEPATH+File.separator+"io.text");
try {
boolean newFile = file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

2.查找指定目录下文件


public static void main(String[] args) {
File file=new File(Constant.FILEPATH);
File[] str = file.listFiles();
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
}

3.文件流-写入


String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
OutputStream outputStream=new FileOutputStream(file,true);
byte[] bytes = "你好".getBytes();
for (int i = 0; i < bytes.length; i++) {
outputStream.write(bytes[i]);
}
outputStream.close();

4.文件流-读取


public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
InputStream inputStream=new FileInputStream(file);
/**
* 单字节读取
*/
{
byte[] bytes = new byte[1024];
int read1;
int count =0;
while((read1 = inputStream.read())!=-1){
bytes[count++]=(byte) read1;
}
System.out.println(new String(bytes));
}
/**
* 多字节读取
*/
{
byte[] bytes=new byte[(int) file.length()];
int read;
while((read=inputStream.read(bytes))!=-1){
System.out.println(new String (bytes));
}
}
}

5.字符流-写入


String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
Writer writer=new FileWriter(file,true);
String str="\r\nhello";
writer.write(str);
writer.close();

6.字符流-读取


public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
Reader read=new FileReader(file);
char[] ch=new char[100];
int temp=0;
int count=0;
while((temp=read.read())!=(-1)){
ch[count++]=(char)temp;
}
read.close();
System.out.println("内容为"+new String(ch,0,count));
}

7.转换流-写入  将输出的字符流转化为字节流


public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
Writer writer=new java.io.OutputStreamWriter(new FileOutputStream(file,true));
writer.write("kobe");
writer.close();
}

8.转换流-读取 将输入的字节流转换为字符流


public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
Reader read = new InputStreamReader(new FileInputStream(file));
char[] b=new char[100];
int len=read.read(b);
System.out.println(new String(b,0,len));
read.close();
}

9.对象流


static String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
static File file=new File(filename);
public static void main(String[] args) throws Exception {
serializable(file);
deserializable(file);
}
/**
* 反序列化
* @param file
* @throws IOException
* @throws ClassNotFoundException
*/
private static void deserializable(File file) throws IOException, ClassNotFoundException {
ObjectInputStream stream=new ObjectInputStream(new FileInputStream(file));
Person o = (Person) stream.readObject();
System.out.println(o.toString());
} /**
* 序列化对象
* @param file
* @throws IOException
*/
private static void serializable(File file) throws IOException {
ObjectOutputStream outputStream=new ObjectOutputStream(new FileOutputStream(file,true));
outputStream.writeObject(new Person("xmz",13));
outputStream.close();
}

10.缓冲字符流-读取


public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
BufferedReader bufferedReader=new BufferedReader(new FileReader(file));
String line;
while((line=bufferedReader.readLine())!=null){//读取一个文本行
System.out.println(line);
}
bufferedReader.close();
}

11.缓冲字符流-写入


public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
FileWriter fileWriter=new FileWriter(file);
/**
* 为了提高写入的效率,使用了字符流的缓冲区。
* 创建了一个字符写入流的缓冲区对象,并和指定要被缓冲的流对象相关联。
*/
BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
bufferedWriter.write("jordan乔丹");
bufferedWriter.newLine();//换行
bufferedWriter.write("kobe蜗壳");
bufferedWriter.write("wade韦德");
bufferedWriter.flush();
bufferedWriter.close(); }

12 管道流-可用于线程通信


static class Send implements Runnable{
private PipedOutputStream out=null;
public Send() {
out=new PipedOutputStream();
}
public PipedOutputStream getOut(){
return this.out;
}
public void run(){
String message="hello,xmz";
try{
out.write(message.getBytes());
}catch (Exception e) {
e.printStackTrace();
}try{
out.close();
}catch (Exception e) {
e.printStackTrace();
}
}
} /**
* 接受消息类
* */
static class Recive implements Runnable{
private PipedInputStream input=null;
public Recive(){
this.input=new PipedInputStream();
}
public PipedInputStream getInput(){
return this.input;
}
public void run(){
byte[] b=new byte[1000];
int len=0;
try{
len=this.input.read(b);
}catch (Exception e) {
e.printStackTrace();
}try{
input.close();
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("接受的内容为 "+(new String(b,0,len)));
}
} public static void main(String[] args) throws IOException {
Send send=new Send();
Recive recive=new Recive();
try{
//管道连接
send.getOut().connect(recive.getInput());
}catch (Exception e) {
e.printStackTrace();
}
new Thread(send).start();
new Thread(recive).start();
}

Java IO API记录的更多相关文章

  1. java io学习记录(路径分隔符)

    java路径分隔符(路径表示) path="E:\\xp\\test\\2.jpg"; path="E:/xp/test/2.jpg"; path=" ...

  2. JAVA NIO学习记录1-buffer和channel

    什么是NIO? Java NIO(New IO)是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java IO API.NIO与原来的IO有同样的作用和目的,但是使用的方式完全不 ...

  3. 系统学习 Java IO (一)----输入流和输出流 InputStream/OutputStream

    目录:系统学习 Java IO ---- 目录,概览 InputStream 是Java IO API中所有输入流的父类. 表示有序的字节流,换句话说,可以将 InputStream 中的数据作为有序 ...

  4. java IO流 之 字节流

    一.file类的常用操作 File file=new File("E:\\test\\javaIo"); System.out.println(file.isDirectory() ...

  5. [Java] Java IO Files

    Files 使用 FileInputStream 或 FileReader 可以用于读入文件,前者基于二进制,后者基于文本.使用它们不需要读取整个文件,但是只能按照它们存储的顺序,依次读取字节,或字符 ...

  6. [Java] Java IO 概况

    Java IO 是 Java 的一套 API, 用于读入和写出数据(输入和输出).Java IO API 位于 java.io package.实际上 java.io package 没有解决所有的输 ...

  7. 系统学习 Java IO (十三)----字符读写 Reader/Writer 及其常用子类

    目录:系统学习 Java IO---- 目录,概览 Reader Reader 类是 Java IO API 中所有 Reader 子类的基类. Reader 类似于 InputStream ,除了它 ...

  8. 系统学习 Java IO (三)----文件类 File

    目录:系统学习 Java IO---- 目录,概览 Java IO API 中的 File 类可以访问基础文件系统. 使用 File 类,可以: 检查文件或目录是否存在. 如果目录不存在,创建一个目录 ...

  9. Java IO: OutputStream

    原文链接 作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) OutputStream类是Java IO API中所有输出流的基类.子类包括Buffere ...

随机推荐

  1. Windows 自动化补丁管理

    Windows 自动化补丁管理 Desktop Central,这一倍受欢迎的补丁管理软件旨在修补可能导致安全薄弱.破坏关键系统数据或导致系统不可用的漏洞.管理此类软件漏洞对网络管理员来说简直是噩梦. ...

  2. SEO常用命令大全

    SEO常用单个命令如下: link: 指某个特定网站的外部链接,这是一个主要获取排名和权重的主要因素. site: 呵呵,这个是经常用到的了,它是指某个特定网站收录情况. 用法:site: www.  ...

  3. Linux学习---自定义数据类型

    struct   结构体 (地址递增) eg:struct myabc{ unsigned int a; unsigned int b; unsigned int c; unsigned int d; ...

  4. Ubuntu18.04安装Guake下拉式终端

    Guake的优势是方便,可以很迅速的唤起与隐藏,已经成为我所需终端的主力. 安装方式:sudo apt-get install guake bug修复:使用时输入exit命令会导致终端卡死系统报错,同 ...

  5. es6面向对象

    <script> class user{ constructor(name,age){ this.name=name; this.age=age; } showName(){ alert( ...

  6. <c:forEach>循环列表,获取勾选的checkbox中某个<td>的值

    <table> <!--列表表头 开始 --> <tr> <th><input type="checkbox" name=&q ...

  7. centos7安装mariadb

    ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) 1.官方um安装mariadb 1).准备官方yum [mariadb ...

  8. 获取列表菜单的选项值与选项以后的VALUE

    <html> <body> <select id="izan" name="" onchange='izzzz()'> &l ...

  9. Android Stdio的问题

    昨天还是可以运行的,今天运行Android Studio,一直提示:Error running app: Instant Run requires 'Tools | android | Enable ...

  10. Charles配置抓取HTTPS请求的Android配置

    关于android手机在mac版charles上抓不到包这个问题困扰了很久,查阅了很多资料,发现是android7.0系统安全策略问题. Charles抓包正常流程1.在手机上配置证书 点击后:直接在 ...