IO
public class Test1 {
public static void main(String[] args) throws IOException {
/*
练习:文件夹拷贝
*/
File src=new File("C:\\aaa\\ccc");
File dest=new File("C:\\aaa\\ddd");
//调用方法
copy(src,dest);//复制文件夹
}
public static void copy(File src,File dest) throws IOException {
//创建dest的文件夹,便于src复制
dest.mkdirs();
//进入数据源
File[] files = src.listFiles();
if (files!=null){
for (File file : files) {
//判断是不是文件夹中是否还有文件夹
if(file.isFile()){
FileInputStream fis=new FileInputStream(file);
FileOutputStream fos=new FileOutputStream(new File(dest,file.getName()));//出口必须也是文件,file.getName()不存在会自动创建
byte []bytes=new byte[1024];
int len;
while((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fis.close();
fos.close();
}else{
//如果是文件夹就递归调用
copy(file,new File(dest,file.getName()));
}
}
}
}
}
--------------------------------
public class Test2 {
public static void main(String[] args) throws IOException {
/*
加密解密文件
使用异或运算
*/
//一个数字异或 同一个数字两次变回本身
System.out.println(100^10);//110
System.out.println(100^10^10);//100
FileInputStream fis=new FileInputStream("C:\\aaa\\b.jpg");
FileOutputStream fos=new FileOutputStream("C:\\aaa\\c.jpg");
int len;
byte[]bytes=new byte[1024*1024];
//拷贝并加密图片
while((len=fis.read(bytes))!=-1){
byte[] bytes1=new byte[len];
for (int i = 0; i < len; i++) {
bytes1[i]=(byte)(bytes[i]^10);
}
fos.write(bytes1,0,len);
}
fis.close();
fos.close();
//最后可删除原文件,想要时解密即可,只需要将文件顺序交换一下即可
}
}
------------------------------------
public class Test3 {
public static void main(String[] args) throws IOException {
//文件排序将a.txt中的2-3-6-4-5-1排序为1-2-3-4-5-6
FileReader fr=new FileReader("day32-code\\src\\a.txt");
StringBuilder sb=new StringBuilder();
int a;
while((a=fr.read())!=-1){
sb.append((char) a);
}
//排序
Integer[] arr = Arrays.stream(sb.toString().split("-")).map(Integer::parseInt).sorted().toArray(Integer[]::new);
//System.out.println(Arrays.toString(arr));
//写入
StringJoiner sj=new StringJoiner("-");
for (int i : arr) {
sj.add(String.valueOf(i));
}
String result=sj.toString();
FileWriter fw=new FileWriter("day32-code\\src\\b.txt");
//写入字符串不用while循环,直接用write方法
fw.write(result);
fr.close();
fw.close();
}
}
--------------------------------
public class BufferedStreamDemo1 {
public static void main(String[] args) throws IOException {
/*
字节缓冲流
拷贝文件
BufferedInputStream(InputStream in)
bufferedOutputStream(OutputStream out)
*/
//1.创建
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("day33-code\\src\\1.txt"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("day33-code\\src\\2.txt"));
//拷贝
//也可以多字节拷贝
int b;
while((b=bis.read())!=-1){
bos.write(b);
}
/*
int len;
byte[] bytes=new byte[1024];
while((len=bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
*/
//3.释放资源
bos.close();
bis.close();
}
}
--------------------------
public class BufferedStreamDemo2 {
public static void main(String[] args) throws IOException {
/*
字符缓冲流
bufferedReader(Reader r)
bufferedWriter(Writer w)
特有方法:
string readLine() 读一行
void newLine() 换行
*/
BufferedReader br=new BufferedReader(new FileReader("day33-code\\src\\1.txt"));
String line;
/*
while((line=br.readLine())!=null){
System.out.println(line);
}
*/
//拷贝
BufferedWriter bw=new BufferedWriter(new FileWriter("day33-code\\src\\2.txt",true));
String line1;
while((line1=br.readLine())!=null){
bw.write(line1);
bw.newLine();
}
bw.close();
br.close();
}
}
---------------------------------------
public class BufferedTest1 {
public static void main(String[] args) throws IOException {
//IO:随用随创建,随用随关闭
/*
将文件中的内容排序:
2.xianghahaxianghahaxianghaha
1.xianghaha666xianghaha666
3.hahahaha666
*/
BufferedReader br = new BufferedReader(new FileReader("day33-code\\src\\1.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("day33-code\\src\\2.txt"));//不建议创建在上方,再用的时候再创建,因为new的是时候会清空文件
Map<Integer, String> map = new TreeMap<>();
String line;
while ((line = br.readLine()) != null) {
String[] split = line.split("\\.");//.前要用转义字符,因为.在正则表达式中有特殊含义
map.put(Integer.parseInt(split[0]), split[1]);
}
br.close();
//写出
Set<Integer> set = map.keySet();
for (Integer i : set) {
bw.write(i + "." + map.get(i));
bw.newLine();
}
bw.close();
}
}
---------------------------
public class ConvertStreamDemo1 {
/*
转换流
InputStreamReader(InputStream in)
*/
public static void main(String[] args) throws IOException {
InputStreamReader isr=new InputStreamReader(new FileInputStream("day33-code\\src\\1.txt"),"GBK");
//读取
int b;
/*while ((b=isr.read())!=-1){
System.out.print((char)b);
}
isr.close();
*/
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("day33-code\\src\\2.txt"),"GBK");
while ((b=isr.read())!=-1){
osw.write(b);
}
osw.close();
//jdk11新写法;
// FileReader fr=new FileReader("day33-code\\src\\1.txt", Charset.forName("GBK"));
//FileWriter fw=new FileWriter("day33-code\\src\\2.txt", Charset.forName("GBK"));
}
}
----------------------------------------
public class ConvertStreamDemo2 {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("day33-code\\src\\1.txt")));
String line;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
----------------------------
public class ObjectStreamDemo1 {
public static void main(String[] args) throws IOException {
/*
序列化流(对象要序列化,必须实现Serializable接口)
objectOutputStream(OutputStream out) 创建一个指定OutputStream的ObjectOutputStream
final void writeObject(Object obj) 将指定的对象写入ObjectOutputStream
*/
//1.创建对象
Student s1=new Student("zhangsan",23);
//2.创建序列化流
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("day33-code\\src\\2.txt"));
//3.写入对象
oos.writeObject(s1);
//4.释放资源
oos.close();
}
}
----------------------------------
public class ObjectStreamDemo2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
/*
反序列化流
ObjectInputStream(InputStream in) 创建一个指定InputStream的ObjectInputStream
object readObject() 从ObjectInputStream读取一个对象
*/
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("day33-code\\src\\2.txt"));
//2.读取对象
Object o = ois.readObject();
System.out.println(o);
//3.释放资源
ois.close();
}
}
-----------------------------
-----
public class Student implements Serializable {
@Serial
private static final long serialVersionUID = -1206037422839780297L;
//private static final long serialVersionUID = 1L;//固定版本号
private String name;
private int age;
//transient关键字
//private transient int age;//不参与序列化
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student{name = " + name + ", age = " + age + "}";
}
}
-----
-----------------------------
public class ObjectStreamDemo3 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
/*
反序列化流
*/
Student s1=new Student("zhangsan",23);
Student s2=new Student("zhang",24);
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("day33-code\\src\\2.txt"));
ArrayList<Student> list1=new ArrayList<>();
list1.add(s1);
list1.add(s2);
oos.writeObject(list1);
oos.close();
//1.创建对象输入流对象
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("day33-code\\src\\2.txt"));
//2.读取对象
ArrayList<Student> list=(ArrayList<Student>) ois.readObject();
System.out.println(list);
//3.释放资源
ois.close();
}
}
--------------------------------
public class PrintStreamDemo1 {
public static void main(String[] args) throws FileNotFoundException {
/*
字节打印流PrintStream
PrintStream(OutputStream /File/String) 创建一个新的打印流
常规方法:write
特有方法(实现数据的原样写出):print/println/
printf(string format,Object...args)
*/
//1.创建打印流
PrintStream ps=new PrintStream("day33-code\\src\\2.txt");
//2.写数据
ps.println(999);
ps.println(true);
ps.printf("%s爱上了%s","haha","xiangxiang");//类似c语言
//3.释放资源
ps.close();
}
}
--------------------------------
public class PrintStreamDemo2 {
public static void main(String[] args) throws FileNotFoundException {
/*
字符打印流PrintWriter
方法类似与字节打印流
*/
//1.创建字符打印流对象
PrintWriter pw=new PrintWriter("day33-code\\src\\2.txt");
//PrintWriter pw=new PrintWriter(new FileWriter("day33-code\\src\\2.txt"),true);
//2.写数据
pw.println(999);
pw.println("hello");
pw.println(true);
//3.释放资源
pw.close();
}
}
-------------------------
public class ZipStreamDemo1 {
public static void main(String[] args) throws IOException {
/*
解压缩流ZipInputStream
*/
//1.创建解压缩流对象
File src=new File("C:\\aaa\\ddd.zip");
//2.创建一个解压到的地址
File dest=new File("C:\\aaa\\");
unZip(src,dest);
}
//定义一个方法来解压
public static void unZip(File src,File dest) throws IOException {
//解压本质:把压缩包中的文件或文件夹读取出来,按层级放到目的地当中
//1.创建解压缩流对象
ZipInputStream zis=new ZipInputStream(new FileInputStream(src), Charset.forName("GBK"));
//先获得压缩包里的每一个zipEntry
//表示在压缩包中读取的文件或文件夹
ZipEntry entry;
while((entry=zis.getNextEntry())!=null){
System.out.println(entry);
//文件夹:需要在目的地创建一个相同的文件夹
//文件:需要读取压缩包中的文件,把文件按层级写到目的地中
if(entry.isDirectory()){
File fr=new File(dest,entry.toString());
fr.mkdirs();
}else{
File fr1=new File(dest,entry.toString());
FileOutputStream fos=new FileOutputStream(fr1);
//读取
int b;
while((b= zis.read())!=-1){
fos.write(b);
}
fos.close();
}
}zis.close();
}
}
-----------------------------
public class ZipStreamDemo2 {
public static void main(String[] args) throws IOException {
/*
压缩流
*/
File src=new File("C:\\aaa\\1.txt");
File dest=new File("C:\\aaa\\");
//调用方法压缩
zip(src,dest);
}
public static void zip(File src,File dest) throws IOException {
//创建压缩流关联压缩包
ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(new File(dest,"1.zip")));
//创建zipentry对象
ZipEntry entry=new ZipEntry("1.txt");
//调用方法
zos.putNextEntry(entry);
//把文件中的数据写入压缩包
FileInputStream fis=new FileInputStream(src);
byte[] bytes=new byte[1024];
int len;
while((len=fis.read(bytes))!=-1){
zos.write(bytes,0,len);
}
//释放资源
fis.close();
//关闭资源
zos.closeEntry();
zos.close();
}
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Hexo!