IO流基础
IoDemo.java
package com.rimi.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class IoDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
//demo01();
//demo02();
//demo03();
//拷贝文本文件,将桌面文件拷贝到项目中
//demo04();
//demo05();
//demo06();
//拷贝文本
//demo07();
//拷贝视频文件,从项目中拷贝到桌面上
//demo08();
//demo09();
//demo10();
//demo11();
//demo12(2,"小聂");
//练习: 在桌面创建一个文件夹,保存输入的学生姓名,再讲文件拷贝到项目中
demo13();
}
//练习: 在桌面创建一个文件夹,保存输入的学生姓名,再讲文件拷贝到项目中
private static void demo13() {
//在桌面创建一个文件夹
File deskFile1= new File("C:\\Users\\rimi\\Desktop\\DemoLianXi");
deskFile1.mkdirs();
//在创建的文件夹中,创建一个文件,用来存储字符流
File file1= new File(deskFile1,"LianXi");
if (!file1.exists()) {
try {
file1.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//输入学生姓名
FileWriter out= null;
try {
out = new FileWriter(file1);
for (int i = 0; i < 3; i++) {
String name = new Scanner(System.in).nextLine();
out.write(name);
out.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (out!=null) {
out.close();
}
} catch (IOException e) {
}
}
//将文件拷贝到指定项目中
FileInputStream in=null;
FileOutputStream out1 = null;
try {
in = new FileInputStream(file1);
out1 = new FileOutputStream("C:\\Users\\rimi\\Desktop\\copy_lianxi");
int num=0;
byte[] buff= new byte[1024*1024];
while ((num=in.read(buff))!=-1) {
out1.write(buff, 0, num);
out1.flush();
}
} catch (FileNotFoundException e) {
System.out.println("找不到文件");
} catch (IOException e) {
System.out.println("获取数据异常");
} finally {
try {
if (out!=null) {
out.close();
}
if (in!=null) {
in.close();
}
} catch (IOException e) {
System.out.println("关闭流");
}
}
}
//修改文件内容
private static void demo12(int index, String newName) {
//创建文件夹
File dier= new File("abc_1234");
dier.mkdirs();
File f1= new File(dier,"stuName.txt");
if (!f1.exists()) {
// 创建文件
try {
f1.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 先要获取
FileReader in = null;
FileWriter out =null;
try {
in = new FileReader(f1);
int num=0;
char[] buff= new char[1024];
String[] names =null;
while((num=in.read(buff))!=-1) {
String con = new String(buff, 0, num);
//将字符串切割
names = con.split("=");
names[index-1] = newName;
out = new FileWriter("stuName.txt");
// 将修改后的数据写入文件
for (String name : names) {
out.write(name+"=");
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (out!=null) {
out.close();
}
if (in!=null) {
in.close();
}
} catch (Exception e2) {
// TODO: handle exception
}
}
}
//输入5个学生姓名保存到硬盘,并可以获取查询
private static void demo11() {
//先创建文件路径对象
File pname=new File("stuName.txt");
FileWriter out=null;
try {
//可以带第二个参数,用于让流写入数据时保留之前的数据
out = new FileWriter(pname,true);
for (int i = 0; i < 3; i++) {
System.out.println("请输入学生姓名");
String name = new Scanner(System.in).nextLine();
out.write(name+"=");
out.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (out!=null) {
try {
out.close();
} catch (IOException e) {
}
}
}
}
//File类的综合使用
private static void demo10() {
//
System.out.println("1--查看当前电脑的所有盘符:");
System.out.println("2--查看指定盘符的文件");
int num = new Scanner(System.in).nextInt();
switch (num) {
case 1:
//查看盘符
File[] roots=File.listRoots();
for (File file : roots) {
System.out.println(file);
}
break;
case 2:
//指定进入盘符
System.out.println("请输入要查看的盘符或路径");
String pf=new Scanner(System.in).nextLine();
//创建文件对象
//File rootF=new File(pf+":"+File.separator);
File rootF=new File(pf);
//判断文件是否存在
if (rootF.exists()) {
return;
}
//判断是文件还是文件夹
if (!rootF.isDirectory()) {
System.out.println("输入的不是文件夹,不能查询子文件");
return;
}
File[] subFiles = rootF.listFiles();
for (File file : subFiles) {
if (file.isDirectory()) {
System.out.println("文件夹:"+file.getName());
}else {
System.out.println("文件:"+file.getName());
}
}
break;
default:
break;
}
}
//File类
private static void demo09() {
File file= new File("C:\\Users\\rimi\\Desktop\\aaa.jpg");
//获取文件名称
file.getName(); //aaa.jpg
//判断当前文件是否存在
file.exists(); //true
file.isFile();//文件true
File f2 = new File("C:\\Users\\rimi\\Desktop\\abc");
f2.getName(); //abc
f2.exists(); //true
f2.isDirectory(); //是文件夹 true
}
//拷贝视频文件,从项目中拷贝到桌面上
private static void demo08() {
//
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("C:\\Users\\rimi\\Desktop\\Java练习\\Day24_IO流的简单用法\\1234.mp4");
out = new FileOutputStream("C:\\Users\\rimi\\Desktop\\copy_12345.mp4");
int num=0;
byte[] buff= new byte[1024*1024];
while ((num=in.read(buff))!=-1) {
out.write(buff, 0, num);
out.flush();
}
} catch (FileNotFoundException e) {
System.out.println("找不到文件");
} catch (IOException e) {
System.out.println("获取数据异常");
} finally {
try {
if (out!=null) {
out.close();
}
if (in!=null) {
in.close();
}
} catch (IOException e) {
System.out.println("关闭流");
}
}
}
//拷贝文本文件时,都建议字节流处理,
private static void demo07() {
FileInputStream in = null;
FileOutputStream out= null;
try {
in = new FileInputStream("C:\\Users\\rimi\\Desktop\\aaa.jpg");
out= new FileOutputStream("copy_1.jpg");
//创建一个记录获取字符个数的变量
int num=0;
//创建一个缓冲数组
byte[] buff=new byte[1024];
while ((num=in.read(buff))!=-1) {
//写入数组
out.write(buff,0, num);
//及时刷新数据
out.flush();
//输出
//System.out.println(new String(buff,0,num));
}
// 由逻辑由内向外关闭流
} catch (FileNotFoundException e) {
System.out.println("找不到文件");
} catch (IOException e) {
System.out.println("读取数据异常");
} finally {
try {
if (out!=null) {
out.close();
}
if (in!=null) {
in.close();
}
} catch (IOException e) {
System.out.println("关闭流");
}
}
}
private static void demo06() {
// TODO Auto-generated method stub
try(FileReader in = new FileReader("demo.txt");) {
//创建字符输入流
int num=0;
//创建一个保存流中读取的数组
char[] buff=new char[1024];
while ((num = in.read(buff))!=-1) {
// System.out.println(num);
// System.out.println(buff);
System.out.println(new String(buff, 0, num));
}
in.close();
} catch (FileNotFoundException e) {
System.out.println("找不到文件");
} catch (IOException e) {
System.out.println("数据读取异常");
}
}
//拷贝文件
private static void demo05() {
// 1.获取源文件内容到内存
FileReader in = null;
FileWriter out= null;
try {
in = new FileReader("C:\\Users\\rimi\\Desktop\\qqq.txt");
out= new FileWriter("qqq_demo.txt");
//创建一个记录获取字符个数的变量
int num=0;
//创建一个缓冲数组
char[] buff=new char[1024];
while ((num=in.read(buff))!=-1) {
//写入数组
out.write(buff,0, num);
//及时刷新数据
out.flush();
//输出
System.out.println(new String(buff,0,num));
}
// 由逻辑由内向外关闭流
} catch (FileNotFoundException e) {
System.out.println("找不到文件");
} catch (IOException e) {
System.out.println("读取数据异常");
} finally {
try {
if (out!=null) {
out.close();
}
if (in!=null) {
in.close();
}
} catch (IOException e) {
System.out.println("关闭流");
}
}
}
private static void demo04() {
// TODO Auto-generated method stub
ArrayList<String> str4=new ArrayList<>();
try {
//创建字符输入流
FileReader in = new FileReader("C:\\Users\\rimi\\Desktop\\qqq.txt");
int num=0;
//创建一个保存流中读取的数组
char[] buff=new char[1024];
while ((num = in.read(buff))!=-1) {
// System.out.println(num);
// System.out.println(buff);
str4.add(new String(buff, 0, num));
System.out.println(new String(buff, 0, num));
}
in.close();
} catch (FileNotFoundException e) {
System.out.println("找不到文件");
} catch (IOException e) {
System.out.println("数据读取异常");
}
try {
//创建一个流
FileWriter out= new FileWriter("demo.txt");
//写入内容
int i=0;
while (i<str4.size()) {
out.write(str4.get(i));
i++;
}
//流使用完后必须立即关闭流,会默认刷新数据
out.close();
} catch (IOException e) {
System.out.println("写入内容失败");
}
}
读取文件
//Reader -->InputStreamReader--->FileReader
//以字符数组的方式读取文件,效率相对高
private static void demo03() {
// TODO Auto-generated method stub
try {
//创建字符输入流
FileReader in = new FileReader("demo.txt");
int num=0;
//创建一个保存流中读取的数组
char[] buff=new char[1024];
while ((num = in.read(buff))!=-1) {
// System.out.println(num);
// System.out.println(buff);
System.out.println(new String(buff, 0, num));
}
in.close();
} catch (FileNotFoundException e) {
System.out.println("找不到文件");
} catch (IOException e) {
System.out.println("数据读取异常");
}
}
//读取文件
//Reader -->InputStreamReader--->FileReader
//一个字符一个字符的读
private static void demo02() {
//
try {
//创建一个字符输入流
FileReader in = new FileReader("demo.txt");
int num=0;
while ((num = in.read())!=-1) {
System.out.print((char)num);
}
in.close();
} catch (FileNotFoundException e) {
System.out.println("找不到文件");
} catch (IOException e) {
System.out.println("数据读取异常");
}
}
/*
* IO流
* 流: Stream
* 输入输出都是参照于内存而言的 从内存中把数据传递给其他设备就是输出
* 从其他设备获取到内存中,就是输入
* Java中流分局方向就分为: 输入流 OutputStream 输入流 InputStream
* 根据流中的数据格式又分为: 字符流 和 字节流
* 方向和数据格式综合Juin产生了4种基本的流类型
* 输出流: (写入) OutputStream(字节输出流) Writer(字符输出流)
* 输入流: (读取) InputStream(字节输入流) Reader(字符输入流)
*/
/*
* 把内存中的文字内容写入到硬盘中(都是以文件形式存在)
* java.io.Writer --->OutputStreamWriter -->FileWriter
*
* Writer
*/
private static void demo01() {
//
try {
//创建一个流
FileWriter out= new FileWriter("demo.txt");
//写入内容
out.write("我的第一次");
out.write(System.lineSeparator());
out.write("IO第一次写入");
out.write(System.lineSeparator());
out.write("IO第一次写入");
//流使用完后必须立即关闭流,会默认刷新数据
out.close();
} catch (IOException e) {
System.out.println("写入内容失败");
}
}
}