1.使用File操作文件
public class IoTest {

    public static void main(String[] args) throws IOException {
/* 01.删除或者创建文件
* File file=new File("e:/io.txt");
addOrDel(file); */ File file=new File("e:/java/hello");
//file.mkdir(); 只能创建一层目录
file.mkdirs(); //同时创建多层目录
} /**
* 删除或者创建文件
*/
public static void addOrDel(File file) throws IOException {
if (!file.exists()) { //判断文件是否存在
if (file.createNewFile()) {//创建成功
System.out.println("创建成功!");
System.out.println("是否是文件:"+file.isFile());
System.out.println("文件名称:"+file.getName());
System.out.println("文件大小:"+file.length());
System.out.println("文件的绝对路径:"+file.getAbsolutePath());
}else {
System.out.println("创建失败");
}
}else {
System.out.println("文件已经存在!");
if (file.delete()) {//删除文件
System.out.println("删除成功!");
}
}
} }
2.使用FileInputStream读取文件内容(字节流)
public class FileInputStreamTest01 {
public static void main(String[] args) throws Exception {
/*
* 创建输入流对象 从电脑中的源文件 获取 数据到 内存中
* 如果没有指定的文件 会报错 FileNotFoundExecption
*/
FileInputStream stream=new FileInputStream("e:/hello.txt");
//输出字节数
System.out.println("可读取的字节数:"+stream.available());
/*
* 读取内容
* 循环读取数据,会从输入流中读取一个8位的字节!把它转换成了0-255之间的整数返回了!
* 我们要想拿到具体的值!必须把整数转成字符 char (0-65535)
* read()当读取文件的结尾处 返回-1
* UTF-8 :中的 中文字符以及中文 占据 3个 字节, 数字和字母占1个字节
* GBK,gb2312: 编码中 中文和符号都占2个字节!
*/
int num;
while((num=stream.read())!=-1){
System.out.print(num);
}
//关闭输入流
stream.close();
} }
3.使用FileOutputStream写入文件内容(字节流)
public class FileOutputStreamTest02 {
public static void main(String[] args) throws Exception {
/*
* 01.java项目的编码格式 要和输出文件的编码一致 都是UTF-8
* 02.如果系统中没有指定的文件,会默认创建
* 03.如果重复输出,则上次的内容会被覆盖
* 如果不想覆盖!使用重载!在第二个参数的位置输入true
*/
FileOutputStream stream=new FileOutputStream("e:/hello.txt",true);
String str="真的好棒!";
//参数传递需要一个Byte类型的数组
stream.write(str.getBytes());
//关闭输出流
stream.close();
}
}
4.使用FileReader读取文件内容(字符流)
public class FileReaderTest03 {
public static void main(String[] args) throws Exception {
//获取当前的编码格式
System.out.println("使用的编码为:"+System.getProperty("file.encoding"));
//创建输入流 Reader
Reader reader=new FileReader("e:/hello.txt");
//因为读取的字符 创建数据的中转站 会有多余的空格产生
char [] words=new char[1024];
int num;
//需要字符串的 拼接
StringBuilder sb=new StringBuilder();
while((num=reader.read(words))!=-1){
sb.append(words);
}
System.out.println(sb.toString());
//关闭输入流
reader.close();
}
}
5.使用BufferedReader读取文件内容(字符流)
public class BufferedReaderTest04 {
public static void main(String[] args) {
/*
* 创建输入流 Reader
* BufferedReader和FileReader联合使用
* 效率高 底层有默认的缓冲区 还可以逐行读取
*/ //创建BufferedReader对象 传递Reader的对象
BufferedReader br=null;
Reader reader=null;
try {
reader = new FileReader("e:/hello.txt");
br=new BufferedReader(reader);
//一行一行的读取
String line=null;
while((line=br.readLine())!=null){
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭流 先开的后关
try {
br.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
6.使用FileWriter读取文件内容(字符流)
public class FileWriterTest05 {
public static void main(String[] args) {
try {
/*
* 输出流 默认的会给我们创建新的文件
* 不使用第二个参数! 那么默认会覆盖之前的内容
* ctrl +shift +t 选中需要查询的类或者接口
*/
Writer writer=new FileWriter("e:/hello.txt",true);
writer.write("我爱北京天安门!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

7.使用BufferedWriter写入文件(字符流)

public class BufferedWriterTest06 {
public static void main(String[] args) {
try {
//创建输出流对象
Writer writer=new FileWriter("e:/hello.txt",true);
//创建BufferedWriter对象
BufferedWriter bw=new BufferedWriter(writer);
//换行
bw.newLine();
bw.write("北京也爱你!");
bw.newLine();
bw.write("北京也爱你!");
bw.close();
writer.close(); //获取输入流
Reader reader=new FileReader("e:/hello.txt");
BufferedReader br=new BufferedReader(reader);
String line=null;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

8.使用InputStreamReader解决中文乱码问题

public class InputStreamReaderTest07 {
public static void main(String[] args) { BufferedReader br=null;
InputStreamReader isr=null;
InputStream stream=null;
try {
//创建输入流对象
stream=new FileInputStream("e:/hello.txt");
System.out.println("文件的大小:"+stream.available());
//使用InputStreamReader来解决乱码
isr=new InputStreamReader(stream, "utf-8");
//读取
br=new BufferedReader(isr);
String line=null;
while((line=br.readLine())!=null){
System.out.println(line);
} } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
isr.close();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

 
 

9.读取2进制文件

public class DataInputStreamTest08 {
public static void main(String[] args) throws Exception {
//创建输入流
InputStream fis=new FileInputStream("e:/mm.mp3");
//读取2进制文件
DataInputStream dis=new DataInputStream(fis); //复制文件到另一个目录
OutputStream fos=new FileOutputStream("e:/U1/慢慢.mp3");
//以2进制的方式输出到指定的目录
DataOutputStream dos=new DataOutputStream(fos); //开始读取
int data;
while((data=dis.read())!=-1){
//写入
dos.write(data);
}
dos.close();
fos.close();
dis.close();
fis.close();
}
10.序列化和反序列化

/**
* 序列化:将内存中对象的状态或者信息 转换成 持久化的过程!
* 反序列化:把持久化的对象 变成 内存中的一个对象的过程!
* 目的:
* 01.使自定义的对象 持久化!对象本身是在内存中的!我们想把它持久化!
* 02.把对象从一个地方传递到另一个地方!
* 03.使程序具有维护性!
*
* 怎么才能实现对象的序列化??
* 1.让对象所属的类 实现Serializable 接口 之后, 这个类 就可以序列化了!
* Serializable:只是一个能否被序列化的标记!
*/
public class Student implements Serializable{ //实体类 private Integer id;
private Integer age;
private String name; @Override
public String toString() {
return "Student [id=" + id + ", age=" + age + ", name=" + name + "]";
}
public Student() {
super();
}
public Student(Integer id, Integer age, String name) {
super();
this.id = id;
this.age = age;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
 

用于序列化的Student类

public class SerializableTest {
public static void main(String[] args) throws Exception { //序列化操作
//首先实例化一个对象
Student student=new Student(1, 500, "小白2");
//想序列化?从内存中 放入 持久化的介质中 输出流
FileOutputStream fos=new FileOutputStream("e:/student.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
//开始持久化
oos.writeObject(student);
oos.close();
fos.close();
}
}
 

序列化

 
/**
* 反序列化
* 需要和 序列化时候的包名 一致 不然 没法反序列化
*/
public class StudentTest {
public static void main(String[] args) throws Exception {
// 从文件中把对象 拿到 内存中 输入流
FileInputStream fis = new FileInputStream("e:/student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
// 读取文件中的对象
Student student = (Student) ois.readObject();
System.out.println(student.getId());
System.out.println(student.getAge());
System.out.println(student.getName());
ois.close();
fis.close();
}
}

io流和序列化的更多相关文章

  1. IO流的序列化和反序列化

    序列化和反序列化的概念: 序列化:把对象转换为字节序列的过程称为对象的序列化.(常见的就是存文件) 反序列化:把字节序列恢复为对象的过程称为对象阿德反序列化. 序列化和反序列化的使用: java.io ...

  2. Java 持久化之 --io流与序列化操作

    1)File类操作文件的属性 1.File类的常用方法 1. 文件的绝对完整路径:getAbsolutePath() 文件名:getName() 文件相对路径:getPath() 文件的上一级目录:g ...

  3. Java 持久化操作之 --io流与序列化

    1)File类操作文件的属性 1.File类的常用方法 1. 文件的绝对完整路径:getAbsolutePath() 文件名:getName() 文件相对路径:getPath() 文件的上一级目录:g ...

  4. Java 基础 IO流之序列化

    一,前言 在前面的IO中,我们都是讲数据以字符串的形式保存.能不能将一个数组保存到文件呢,当取出数据时也是一个数组,如果能够实现那就完美了.我们都知道比较通用的有JSON格式的序列化,那java中也有 ...

  5. java 21 - 13 IO流之序列化和反序列化

    序列化流:把对象按照流一样的方式存入文本文件或者在网络中传输.对象 -- 流数据(ObjectOutputStream) 构造方法:ObjectInputStream(InputStream in) ...

  6. IO流之序列化流与反序列化流

    序列化流与反序列化流 用于从流中读取对象的 操作流 ObjectInputStream    称为 反序列化流 用于向流中写入对象的操作流 ObjectOutputStream   称为 序列化流 l ...

  7. java ->IO流_序列化流与反序列化流

    序列化流与反序列化流 用于从流中读取对象的操作流 ObjectInputStream    称为 反序列化流 用于向流中写入对象的操作流 ObjectOutputStream   称为 序列化流(对象 ...

  8. IO流(03)--序列化流、打印流

    序列化流 Java提供了一种对象序列化的机制,用一个字节序列可以表示一个对象,该字节序列包含该对象的数据.对象的类型和对象中存储的属性等信息.字节序列写入到文件中后,就相当于在文件中保存了一个对象信息 ...

  9. java io流与序列化反序列化

    java的io是实现输入和输出的基础,可以方便的实现数据的输入和输出操作. 序列化 (Serialization)是将对象的状态信息转换为可以存储或传输的形式的过程.在序列化期间,对象将其当前状态写入 ...

随机推荐

  1. Word批量删除所有书签

    Word中的书签功能可快速.准确定位文档中特定的位置,经常用于模板定制.文档产出等. 可一直以来,书签功能存在一个不便的操作,即无法批量删除,只能单个删除,操作极不友好. 解决方案 我用代码暂时还改变 ...

  2. 使用Guava的ComparisonChain实现自定义的排序

    可以看到使用比较器前,先要写一个实体类,还要实现comparable接口,实现compareTo方法.这个方法一般会返回-1 0 1三个int类型数字,分别表示,对象和传入的对象比较,排序应该在传入的 ...

  3. 视觉SLAM中的数学基础 第三篇 李群与李代数

    视觉SLAM中的数学基础 第三篇 李群与李代数 前言 在SLAM中,除了表达3D旋转与位移之外,我们还要对它们进行估计,因为SLAM整个过程就是在不断地估计机器人的位姿与地图.为了做这件事,需要对变换 ...

  4. maven超级pom内容

    1.位置 2.内容 <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the A ...

  5. IPV4地址分类

    IPV4地址的分类 私网地址: 10.0.0.0/8                                       //A类地址 172.16.0.0/16-172.31.0.0/16  ...

  6. 如何免费的让网站启用https

    本文源自酷壳:如何免费的让网站启用HTTPS 今天,我把CoolShell变成https的安全访问了.我承认这件事有点晚了,因为之前的HTTP的问题也有网友告诉我,被国内的电信运营商在访问我的网站时加 ...

  7. 给iOS开发者的Android开发建议

    本人从事iOS应用开发已经5年有余,直到现在还总是刻意回避Andriod应用的开发.但是不管你信不信,安卓开发还是很有意思的,从iOS转向Android应用开发的跨度并没有你想象的那么大. 现在我把在 ...

  8. 浅析Sql Server参数化查询

    说来惭愧,工作差不多4年了,直到前些日子被DBA找上门让我优化一个CPU占用很高的复杂SQL语句时,我才突然意识到了参数化查询的重要性. 相信有很多开发者和我一样对于参数化查询认识比较模糊,没有引起足 ...

  9. 《Redis入门指南(第2版)》读后感

    今天刚刚将此书看完,现在还能记住一些内容,还有一些感慨感想,正好又想写点什么了就随便记录一下吧!也许灵感明天就消失了呢? 首先觉得作者非常的厉害,年纪轻轻的就写出了这么一本非常不错的书籍! 然后就是对 ...

  10. Atitit web remote远程调试的原理attilax总结

    Atitit web remote远程调试的原理attilax总结 Jvm是vm打开一个debug port,然后ide先连接..然后执行url,就会vm会与ide沟通.. Php的xdebug po ...