package cn.temptation;

 public class Sample01 {
public static void main(String[] args) {
// 需求:继承关系中爷爷类、父类、子类,现在父类行为有了增强,如何不影响子类?
// 思路:首先考虑让父类实现增强能力的接口,但是这样做,会让继承子类也具有增强能力
// Son son = new Son();
// son.method(); // 其次考虑让父类中的增强行为方法设置为private,这样子类是没有继承,但是外部访问父类对象的该方法也无法访问
// Father father = new Father();
// 语法错误:The method methodEx() from the type Father is not visible
// father.methodEx(); // 再次考虑对父类使用final关键字,这就让父类断子绝孙,没有子类能继承了;
// 如果对父类的成员方法使用final关键字,只是不让子类去重写继承过来的父类的成员方法,并不是不能使用
// Son son = new Son();
// son.show();
}
} //interface IAbility {
// public abstract void method();
//}
//
//class Father implements IAbility {
// @Override
// public void method() {
//
// }
//
// private void methodEx() {
//
// }
//
// public final void show() {
//
// }
//}
//
//class Son extends Father {
// // 语法错误:Cannot override the final method from Father
//// @Override
//// public void show() {
////
//// }
//}
 package cn.temptation;

 public class Sample02 {
public static void main(String[] args) {
// 需求:继承关系中爷爷类、父类、子类,现在父类行为有了增强,如何不影响子类? // 思路:这种共性问题,同样通过设计模式解决,使用"装饰模式(修饰模式)"
// 制作和继承链无关的新的类,对有增强行为的类进行装饰(修饰) // 继承链上的子类的行为不受影响
Son son = new Son();
son.methodBySon();
son.methodByFather();
son.methodByGrandFather();
// 显然父类的增强行为和继承子类无关
// 语法错误:The method methodEx() is undefined for the type Son
// son.methodEx();
System.out.println("-----------------"); // 使用行为未增强的父类
Father father = new Father();
father.methodByFather();
father.methodByGrandFather();
System.out.println("-----------------"); // 使用行为增强的父类
FatherEx fatherEx = new FatherEx(new Father());
// 调用其增强的行为
fatherEx.methodEx();
// 调用之前的父类的行为
fatherEx.getFather().methodByFather();
fatherEx.getFather().methodByGrandFather();
}
} class GrandFather {
public void methodByGrandFather() {
System.out.println("爷爷类的成员方法");
}
} class Father extends GrandFather {
public void methodByFather() {
System.out.println("父类的成员方法");
}
} /**
* 对Father类进行装饰
* 既然是对Father类的对象进行装饰,那么应该有Father类的实例对象
* 考虑通过参数传入,选择最简单的构造函数的参数传入
*/
class FatherEx {
// 成员变量
private Father father = null; // 构造函数
public FatherEx(Father father) {
this.father = father;
} // 成员方法
public Father getFather() {
return father;
} public void setFather(Father father) {
this.father = father;
} /**
* 父类的增强行为
*/
public void methodEx() {
System.out.println("父类的增强成员方法");
}
} class Son extends Father {
public void methodBySon() {
System.out.println("子类的成员方法");
}
}
 package cn.temptation;

 import java.io.File;
import java.io.IOException; public class Sample03 {
public static void main(String[] args) {
/*
* 为了实现IO(Input/Output)的操作,对硬盘上的文件的形式有一个了解,即 目录(文件夹) 和 文件
* Java语言中提供了 File类 方便操作目录 和 文件
*
* 类 File:文件和目录路径名的抽象表示形式。
*
* 构造函数:
* 1、File(String pathname):通过将给定路径名字符串转换为抽象路径名来创建一个新 File实例。
* 2、File(String parent, String child):根据 parent 路径名字符串和 child 路径名字符串创建一个新 File实例。
* 3、File(File parent, String child):根据 parent 抽象路径名和 child 路径名字符串创建一个新 File实例。
*
* 成员方法:
* 1、boolean createNewFile():当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
* 返回:如果指定的文件不存在并成功地创建,则返回 true;如果指定的文件已经存在,则返回 false
*/ // 因为路径字符串中存在反斜杠,所以需要进行转义
// 语法错误:Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
// File file1 = new File("D:\abc.txt"); // 正确写法:使用两个反斜杠转义反斜杠
File file1 = new File("D:\\abc.txt");
System.out.println(file1); // D:\abc.txt // 下面语句第一次执行会返回true的结果,再次执行会返回false的结果
try {
System.out.println(file1.createNewFile());
} catch (IOException e) {
e.printStackTrace();
} File file2 = new File("D:\\iotest", "abc.txt");
System.out.println(file2); // D:\iotest\abc.txt // 执行出错:java.io.IOException: 系统找不到指定的路径。
// 因为没有iotest这个目录,手工创建iotest目录后,再执行,返回true的结果
try {
System.out.println(file2.createNewFile());
} catch (IOException e) {
e.printStackTrace();
} File file3 = new File("D:\\iotest");
File file4 = new File(file3, "abc.txt");
System.out.println(file4); // D:\iotest\abc.txt try {
System.out.println(file4.createNewFile());
} catch (IOException e) {
e.printStackTrace();
} /*
* File类的常用字段:
* static String separator:与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串。
*/
File file5 = new File("d:" + File.separator + "iotest" + File.separator + "abc.txt");
System.out.println(file5); // d:\iotest\abc.txt try {
System.out.println(file5.createNewFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}
 package cn.temptation;

 import java.io.File;
import java.io.IOException; public class Sample04 {
public static void main(String[] args) {
/*
* File类的常用成员方法:
* 1、boolean mkdir():创建此抽象路径名指定的目录。
*/
// 创建目录
File file1 = new File("D:\\iotest");
System.out.println(file1.mkdir()); // 创建文件
File file2 = new File("D:\\iotest\\test.txt");
try {
System.out.println(file2.createNewFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}
 package cn.temptation;

 import java.io.File;

 public class Sample05 {
public static void main(String[] args) {
/*
* File类的常用成员方法:
* 1、boolean mkdirs():创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
*/ // 需求:创建这样的目录:D:\aaa\bbb\ccc
// File file1 = new File("D:\\aaa\\bbb\\ccc");
// System.out.println(file1.mkdir()); // false,使用mkdir创建多级目录,不可行 // 如下一级一级的创建目录可行,但是太麻烦
// File file2 = new File("D:\\aaa");
// System.out.println(file2.mkdir());
// File file3 = new File("D:\\aaa\\bbb");
// System.out.println(file3.mkdir());
// File file4 = new File("D:\\aaa\\bbb\\ccc");
// System.out.println(file4.mkdir()); // 使用mkdirs创建多级目录
// File file5 = new File("D:\\aaa\\bbb\\ccc");
// System.out.println(file5.mkdirs()); // 问题:如下语句会执行出什么样的结果?
// 答:返回false的结果,不会产生iotest目录以及在其中产生test.txt目录
File file6 = new File("D:\\iotest\test.txt");
System.out.println(file6.mkdirs()); // false // 注意:进行IO操作之前,首先要确定生成的是目录还是文件
}
}
 package cn.temptation;

 import java.io.File;
import java.io.IOException; public class Sample06 {
public static void main(String[] args) {
/*
* File类的常用成员方法:
* 1、boolean delete():删除此抽象路径名表示的文件或目录。
*/ // 创建目录
File file1 = new File("D:\\iotest");
System.out.println("创建目录:" + file1.mkdir()); // 创建目录:true // 在创建出来的目录下创建文件
File file2 = new File("D:\\iotest\\test.txt");
try {
System.out.println("创建文件:" + file2.createNewFile()); // 创建文件:true
} catch (IOException e) {
e.printStackTrace();
} // 删除非空目录iotest
System.out.println("删除目录:" + file1.delete()); // 删除目录:false // 删除非空目录中的文件,再删除这个目录
System.out.println("先删除文件:" + file2.delete()); // 先删除文件:true
System.out.println("再删除目录:" + file1.delete()); // 再删除目录:true
}
}
 package cn.temptation;

 import java.io.File;
import java.io.IOException; public class Sample07 {
public static void main(String[] args) {
/*
* File类使用时,如果不带盘符及路径名,生成的目录 或 文件 在当前的工程目录下
*/
File file1 = new File("iotest");
System.out.println(file1.mkdir()); File file2 = new File("abc.txt");
try {
System.out.println(file2.createNewFile());
} catch (IOException e) {
e.printStackTrace();
} File file3 = new File("iotest\\abc.txt");
try {
System.out.println(file3.createNewFile());
} catch (IOException e) {
e.printStackTrace();
} File file4 = new File("iotest\\aaa\\bbb\\ccc");
System.out.println(file4.mkdirs()); System.out.println("删除文件:" + file2.delete()); // 对于有子目录的目录,删除时同样也要先删除其子目录
System.out.println("删除目录:" + (new File("iotest\\aaa")).delete()); // 删除目录:false
}
}
 package cn.temptation;

 import java.io.File;
import java.io.IOException; public class Sample08 {
public static void main(String[] args) {
/*
* File类的常用成员方法:
* 1、boolean renameTo(File dest):重新命名此抽象路径名表示的文件。
*
* 注意:
* 1、如果路径名称相同,renameTo方法做的是改名的操作
* 2、如果路径名称不相同,renameTo方法做的不仅改名,而且剪切并粘贴到新的路径下
*/
File file1 = new File("a.txt");
File file2 = new File("b.txt"); // 先创建出a.txt文件
try {
System.out.println("创建a.txt:" + file1.createNewFile()); // 创建a.txt:true
} catch (IOException e) {
e.printStackTrace();
} // 再重新命名
System.out.println("a.txt改名为b.txt:" + file1.renameTo(file2)); // a.txt改名为b.txt:true File file3 = new File("D:\\c.txt");
// 再重新命名
System.out.println("a.txt改名为D:\\c.txt:" + file1.renameTo(file3)); // a.txt改名为D:\c.txt:false
System.out.println("b.txt改名为D:\\c.txt:" + file2.renameTo(file3)); // b.txt改名为D:\c.txt:true
}
}
 package cn.temptation;

 import java.io.File;
import java.io.IOException; public class Sample09 {
public static void main(String[] args) {
/*
* File类的常用成员方法:
*
* 判断功能:
* 1、boolean isDirectory():测试此抽象路径名表示的文件是否是一个目录。
* 2、boolean isFile():测试此抽象路径名表示的文件是否是一个标准文件。
* 3、boolean isHidden():测试此抽象路径名指定的文件是否是一个隐藏文件。
* 4、boolean exists():测试此抽象路径名表示的文件或目录是否存在。
* 5、boolean canRead():测试应用程序是否可以读取此抽象路径名表示的文件。
* 6、boolean canWrite():测试应用程序是否可以修改此抽象路径名表示的文件。
*/ File file = new File("test.txt"); try {
System.out.println("创建文件:" + file.createNewFile());
} catch (IOException e) {
e.printStackTrace();
} System.out.println("isDirectory:" + file.isDirectory()); // isDirectory:false
System.out.println("isFile:" + file.isFile()); // isFile:true
System.out.println("isHidden:" + file.isHidden()); // isHidden:false
System.out.println("exists:" + file.exists()); // exists:true
System.out.println("canRead:" + file.canRead()); // canRead:true
System.out.println("canWrite:" + file.canWrite()); // canWrite:true
}
}
 package cn.temptation;

 import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date; public class Sample10 {
public static void main(String[] args) {
/*
* File类的常用成员方法:
*
* 获取功能:
* 1、String getAbsolutePath():返回此抽象路径名的绝对路径名字符串。
* 2、String getPath():将此抽象路径名转换为一个路径名字符串。
* 3、String getName():返回由此抽象路径名表示的文件或目录的名称。
* 4、long length():返回由此抽象路径名表示的文件的长度。
* 5、long lastModified():返回此抽象路径名表示的文件最后一次被修改的时间。
*/ File file = new File("test.txt"); try {
System.out.println("创建文件:" + file.createNewFile());
} catch (IOException e) {
e.printStackTrace();
} System.out.println("getAbsolutePath:" + file.getAbsolutePath()); // getAbsolutePath:D:\workspace\Day20170920_IO\test.txt
System.out.println("getPath:" + file.getPath()); // getPath:test.txt
System.out.println("getName:" + file.getName()); // getName:test.txt
System.out.println("length:" + file.length()); // length:0
System.out.println("lastModified:" + file.lastModified()); // lastModified:1505876385640 // 转换为日期时间
Date date = new Date(1505876385640L);
System.out.println(date); // Wed Sep 20 10:59:45 CST 2017
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date)); // 2017-09-20 10:59:45
}
}
 package cn.temptation;

 import java.io.File;

 public class Sample11 {
public static void main(String[] args) {
/*
* File类的常用成员方法:
*
* 获取功能:
* 1、String[] list():返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。
* 2、File[] listFiles():返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
*/ File file = new File("C:\\");
String[] arr = file.list(); // 隐藏目录 和 文件均可以获取到 for (String item : arr) {
System.out.println(item);
} System.out.println("----------------"); File[] files = file.listFiles(); for (File item : files) {
System.out.println(item);
}
}
}
 package cn.temptation;

 import java.io.File;

 public class Sample12 {
public static void main(String[] args) {
// 需求:查找C盘根目录下是否有后缀名为.jpg的文件,如果有就显示出来 // 思路:
// 1、获取C盘根目录的File对象
// 2、获取C盘根目录的所有目录和文件
// 3、使用File类的判断功能isFile方法判断是否为文件
// 4、如果是文件,再判断后缀名为.jpg(根据String类的成员方法:boolean endsWith(String suffix)测试此字符串是否以指定的后缀结束。 ) File file = new File("C:\\"); File[] files = file.listFiles(); for (File item : files) {
if (item.isFile()) {
if (item.getName().endsWith(".jpg")) {
System.out.println(item);
}
}
}
}
}
 package cn.temptation;

 import java.io.File;
import java.io.FilenameFilter; public class Sample13 {
public static void main(String[] args) {
/*
* File类的常用成员方法:
* 1、File[] listFiles(FilenameFilter filter):返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件和目录。
*
* 接口 FilenameFilter:实现此接口的类实例可用于过滤器文件名。Abstract Window Toolkit 的文件对话框组件使用这些实例过滤 File 类的 list 方法中的目录清单。
*
* FilenameFilter接口的常用成员方法:
* 1、boolean accept(File dir, String name):测试指定文件是否应该包含在某一文件列表中。
* 参数:dir - 被找到的文件所在的目录。 name - 文件的名称。
*/ File file = new File("C:\\"); String[] arr = file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
boolean result = false; // 写法1
// if (new File(dir, name).isFile()) {
// if (name.endsWith(".jpg")) {
// result = true;
// }
// } // 写法2
// result = new File(dir, name).isFile() && name.endsWith(".jpg");
//
// return result; // 写法3
return new File(dir, name).isFile() && name.endsWith(".jpg");
}
}); for (String item : arr) {
System.out.println(item);
}
}
}
 package cn.temptation;

 import java.io.File;
import java.util.Scanner; public class Sample14 {
public static void main(String[] args) {
// 需求:键盘录入一个路径和文件后缀名,查找该路径下所有该文件后缀名的文件并显示出来
// 比如:查找D:\workspace下的所有.java文件,注意可能是多级目录 // 思路:
// 因为获取指定路径的File对象可能是一个目录,也可能是一个文件,如果是文件,只要判断一下是否为指定后缀名的文件即可
// 如果是目录,获取目录中内容进行判断
// 显然,多级目录的操作考虑使用递归 // 接收键盘的录入
Scanner input = new Scanner(System.in);
System.out.println("输入一个路径:(例如:D:\\workspace)");
String path = input.nextLine();
System.out.println("输入文件后缀名:(例如:.java)");
String suffix = input.nextLine();
input.close(); // 创建File对象
File file = new File(path); // 如下代码,如果找到的File对象是目录时,还要再查找目录中的内容进行判断
// 所以考虑使用递归进行改写
// 使用递归时,注意递归的终止条件,否则会形成死循环
// 这里递归的终止条件是找到的File对象是文件就不再递归
// File[] files = file.listFiles();
// for (File item : files) {
// if (item.isFile()) { // 是文件
// if (item.getName().endsWith(suffix)) {
// System.out.println(item);
// }
// } else { // 是目录
// File[] sonFiles = item.listFiles();
// for (File sonItem : sonFiles) {
// if (sonItem.isFile()) { // 是文件
// if (sonItem.getName().endsWith(suffix)) {
// System.out.println(sonItem);
// }
// } else { // 是目录
// sonItem.listFiles();
// }
// }
// }
// } // 调用递归方法
getAllFolderAndFile(file, suffix); // 注意:
// 1、该操作传入的文件路径不应该是只有盘符,
// 因为windows操作系统会在每个盘符下都创建一个回收站目录,该目录使用listFiles时会得到null的结果,进而产生空指针异常
// 2、手工创建空目录,使用listFiles时会得到空的File数组,遍历不会出错
} /**
* 获取所有目录和文件
* @param file File对象
* @param suffix 文件后缀名
*/
public static void getAllFolderAndFile(File file, String suffix) {
File[] files = file.listFiles();
for (File item : files) {
if (item.isFile()) { // 是文件
if (item.getName().endsWith(suffix)) {
System.out.println(item);
}
} else { // 是目录
getAllFolderAndFile(item, suffix);
}
}
}
}
 package cn.temptation;

 import java.io.FileOutputStream;
import java.io.IOException; public class Sample15 {
public static void main(String[] args) throws IOException {
/*
* IO流:
*
* 现实世界中有很多流的概念:水流、气流、电流,都是物质的移动
* Java中的流是为了实现数据的移动
*
* 李白的诗句:"黄河之水天上来,奔流到海不复回"
* 源头:天上 目标(终点):海
*
* 数据流也是流,也有源头 和 终点,应用程序像水坝一样拦在中间
*
* 站在应用程序的角度,将流分为:
* 1、按流向分类:
* A:输入流----->输入给应用程序(读取数据)----->从源头读取数据
* B:输出流----->输出给目标(写出数据)----->写出数据给目标
*
* 2、按数据类型分类:
* A:字节流(1字节 = 8bit)
* 字节输入流:读取数据-----> InputStream 此抽象类是表示字节输入流的所有类的超类。
* 字节输出流:写出数据-----> OutputStream 此抽象类是表示输出字节流的所有类的超类。
* B:字符流(1字符=2字节=16bit)(注意:如果使用UTF-8字符集,非英文数字字符使用3个字节存储)
* 字符输入流:读取数据-----> Reader 用于读取字符流的抽象类。
* 字符输出流:写出数据-----> Writer 用于写入字符流的抽象类。
*
*
*
* 类 OutputStream:此抽象类是表示输出字节流的所有类的超类。输出流接受输出字节并将这些字节发送到某个接收器。
* 需要定义 OutputStream 子类的应用程序必须始终提供至少一种可写入一个输出字节的方法。
*
* 类 FileOutputStream:文件输出流是用于将数据写入 File 或 FileDescriptor 的输出流。
*
* FileOutputStream类的构造函数:
* FileOutputStream(String name):创建一个向具有指定名称的文件中写入数据的输出文件流。
*
* FileOutputStream类的常用成员方法:
* 1、void close():关闭此文件输出流并释放与此流有关的所有系统资源。
* 2、void write(byte[] b):将 b.length个字节从指定byte数组写入此文件输出流中。
*/ FileOutputStream fos = new FileOutputStream("D:\\test01.txt"); fos.write("abc".getBytes()); // 释放资源
fos.close();
}
}
 package cn.temptation;

 import java.io.FileOutputStream;
import java.io.IOException; public class Sample16 {
public static void main(String[] args) throws IOException {
/*
* FileOutputStream类的常用成员方法:
* 1、void write(int b):将指定字节写入此文件输出流。
* 2、void write(byte[] b):将 b.length 个字节从指定 byte 数组写入此文件输出流中。
* 3、void write(byte[] b, int off, int len):将指定 byte 数组中从偏移量 off开始的 len个字节写入此文件输出流。
*/ FileOutputStream fos = new FileOutputStream("D:\\test01.txt"); // 写出一个字节
fos.write(97); // 先写出一个a byte[] arr = { 97, 98, 99 };
fos.write(arr); // 接着写出abc fos.write(arr, 1, 1); // 接着写出b // 释放资源
fos.close();
}
}
 package cn.temptation;

 import java.io.FileOutputStream;
import java.io.IOException; public class Sample17 {
public static void main(String[] args) throws IOException {
/*
* FileOutputStream类的构造函数:
* 1、FileOutputStream(File file, boolean append):创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
* 参数:file - 为了进行写入而打开的文件。 append - 如果为 true,则将字节写入文件末尾处,而不是写入文件开始处。
*/ // 接着上一个例子,test01.txt中原先有内容为"aabcb",设置构造函数的append参数为true,不会覆盖之前的内容,在文件末尾追加
// 设置构造函数的append参数为false,覆盖之前的内容
// FileOutputStream fos = new FileOutputStream("D:\\test01.txt", true); // 写入的结果为:aabcb添加的内容0添加的内容1添加的内容2
FileOutputStream fos = new FileOutputStream("D:\\test01.txt", false); // 写入的结果为:添加的内容0添加的内容1添加的内容2 for (int i = 0; i < 3; i++) {
fos.write(("添加的内容" + i).getBytes());
} fos.close();
}
}
 package cn.temptation;

 import java.io.FileOutputStream;
import java.io.IOException; public class Sample18 {
public static void main(String[] args) {
// 需求:使用try...catch...finally的形式使用FileOutputStream
FileOutputStream fos = null; try {
fos = new FileOutputStream("D:\\test01.txt"); fos.write("abc".getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
 package cn.temptation;

 import java.io.FileInputStream;
import java.io.IOException; public class Sample19 {
public static void main(String[] args) throws IOException {
/*
* 类 FileInputStream:从文件系统中的某个文件中获得输入字节。哪些文件可用取决于主机环境。
* FileInputStream用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。
*
* FileInputStream类的构造函数:
* FileInputStream(String name):通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。
*
* FileInputStream类的常用成员方法:
* 1、void close():关闭此文件输入流并释放与此流有关的所有系统资源。
* 2、int read():从此输入流中读取一个数据字节。
* 返回:下一个数据字节;如果已到达文件末尾,则返回 -1。
*/ // test01.txt文件中有内容为"abc"
FileInputStream fis = new FileInputStream("D:\\test01.txt"); // 下句从输入流中读取一个数据字节
// System.out.println(fis.read()); // 97 // 循环读取
// 如下写法错误,一次循环中调用了两次read方法,对于"abc"的读取,会得到"98、-1"的结果
// while (fis.read() != -1) {
// System.out.println(fis.read());
// } // 正确写法
int ch = 0;
while ((ch = fis.read()) != -1) {
System.out.println((char) ch);
} fis.close();
}
}
 package cn.temptation;

 import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class Sample20 {
public static void main(String[] args) throws IOException {
// 需求:从test01.txt文件中使用字节流读取内容,再使用字节流写入到test02.txt文件中 // 思路:
// 源头:test01.txt----->读取数据,使用FileInputStream
// 目标:test02.txt----->写出数据,使用FileOutputStream FileInputStream fis = new FileInputStream("D:\\test01.txt");
FileOutputStream fos = new FileOutputStream("D:\\test02.txt"); // 一边读,一边写
int ch = 0;
while ((ch = fis.read()) != -1) {
// 注意:如果test01.txt中写的是中文内容,读取时会显示为乱码(不论是否修改txt文件的编码格式),但是写出到test02.txt中还会时正常的中文
System.out.println((char)ch);
// 伴随着读的操作就去做写的操作
fos.write(ch);
} fos.close();
fis.close();
}
}
 package cn.temptation;

 import java.util.Arrays;

 public class Sample21 {
public static void main(String[] args) {
/*
* 在UTF-8编码下存储非英文非数字字符,每一个非英数字符会占用三个字节存储
*/
String str = "中国";
byte[] arr = str.getBytes(); for (byte item : arr) {
System.out.print(item); // -28-72-83-27-101-67
} System.out.println(); System.out.println(Arrays.toString(arr)); // [-28, -72, -83, -27, -101, -67]
}
}
 package cn.temptation;

 import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class Sample22 {
public static void main(String[] args) throws IOException {
// 需求:读取图片"陆逊.jpg",写出成新的图片"偶像.jpg"
FileInputStream fis = new FileInputStream("D:\\陆逊.jpg");
FileOutputStream fos = new FileOutputStream("D:\\偶像.jpg"); // 一边读,一边写
int ch = 0;
while ((ch = fis.read()) != -1) {
// 伴随着读的操作就去做写的操作
fos.write(ch);
} fos.close();
fis.close();
}
}
 package cn.temptation;

 import java.io.FileInputStream;
import java.io.IOException; public class Sample23 {
public static void main(String[] args) throws IOException {
/*
* FileInputStream类的常用成员方法:
* 1、int read(byte[] b):从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
* 参数:b - 存储读取数据的缓冲区。数组的长度一般是1024 或 1024的整数倍
* 返回:读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。
*
* String类的构造函数:
* 1、String(byte[] bytes, int offset, int length):通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String。
*/
FileInputStream fis = new FileInputStream("D:\\test01.txt"); byte[] arr = new byte[1024];
int ch = 0; // while ((ch = fis.read()) != -1) {
// System.out.println((char)ch); // a b c
// } while ((ch = fis.read(arr)) != -1) {
System.out.println(ch); // 读入缓冲区的字节总数为3
System.out.println(new String(arr, 0, ch)); // abc
} fis.close();
}
}
 package cn.temptation;

 import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class Sample24 {
public static void main(String[] args) throws IOException {
// 需求:从test01.txt文件中读取内容,写出到test02.txt文件中,使用缓冲区 FileInputStream fis = new FileInputStream("D:\\test01.txt");
FileOutputStream fos = new FileOutputStream("D:\\test02.txt"); // 一边读,一边写
byte[] arr = new byte[1024];
int length = 0; while ((length = fis.read(arr)) != -1) {
// 伴随着读的操作就去做写的操作
// 写法1
// fos.write(arr);
// 写法2
fos.write(arr, 0, length);
} fos.close();
fis.close();
}
}
 package cn.temptation;

 import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class Sample25 {
public static void main(String[] args) throws IOException {
/*
* 类 FilterOutputStream:此类是过滤输出流的所有类的超类。这些流位于已存在的输出流(基础 输出流)之上,它们将已存在的输出流作为其基本数据接收器,但可能直接传输数据或提供一些额外的功能。
*
* 类 BufferedOutputStream:实现缓冲的输出流。通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统。
*
* BufferedOutputStream类的构造函数:
* BufferedOutputStream(OutputStream out):创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
* BufferedOutputStream(OutputStream out, int size):创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。
*
* BufferedOutputStream类的常用成员方法:
* 1、void write(int b) :将指定的字节写入此缓冲的输出流。
* 2、void flush():刷新此缓冲的输出流。迫使所有缓冲的输出字节被写出到底层输出流中。
*/ FileOutputStream fos = new FileOutputStream("D:\\test01.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos); // 写出数据并做强制刷新出去
bos.write("test".getBytes());
bos.flush(); bos.close();
fos.close();
}
}
 package cn.temptation;

 import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException; public class Sample26 {
public static void main(String[] args) throws IOException {
/*
* 类 FilterInputStream:包含其他一些输入流,它将这些流用作其基本数据源,它可以直接传输数据或提供一些额外的功能。
* 继承自InputStream抽象类
*
* 类BufferedInputStream:为另一个输入流添加一些功能,即缓冲输入以及支持 mark 和 reset 方法的能力。
*
* BufferedInputStream类的构造函数:
* 1、BufferedInputStream(InputStream in):创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
* 2、BufferedInputStream(InputStream in, int size):创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
*
* BufferedInputStream类的常用成员方法:
* 1、int read():参见 InputStream 的 read 方法的常规协定。
* 2、int read(byte[] b, int off, int len):从此字节输入流中给定偏移量处开始将各字节读取到指定的 byte 数组中。
* 参数:b - 目标缓冲区。 off - 开始存储字节处的偏移量。 len - 要读取的最大字节数。
* 返回:读取的字节数;如果已到达流末尾,则返回 -1。
*/ BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\test01.txt")); // 写法1、读取一个字节
// int ch = 0;
//
// while ((ch = bis.read()) != -1) {
// System.out.println((char)ch);
// } // 写法2、读取一个字节数组(缓冲区对象)
byte[] arr = new byte[1024];
int length = 0; while ((length = bis.read(arr)) != -1) {
System.out.println(length); // 缓冲区中读取的总字节数:4
System.out.println(new String(arr, 0, length)); // test
} bis.close();
}
}
 package cn.temptation;

 import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class Sample27 {
public static void main(String[] args) throws IOException {
// 比较IO操作的耗时
long startTime = System.currentTimeMillis(); // method1("D:\\KuGou\\田馥甄 - 傻子.mp3", "D:\\田馥甄 - 傻子.mp3");
// method2("D:\\KuGou\\田馥甄 - 傻子.mp3", "D:\\田馥甄 - 傻子.mp3");
// method3("D:\\KuGou\\田馥甄 - 傻子.mp3", "D:\\田馥甄 - 傻子.mp3");
method4("D:\\KuGou\\田馥甄 - 傻子.mp3", "D:\\田馥甄 - 傻子.mp3"); long endTime = System.currentTimeMillis(); System.out.println("耗时:" + (endTime - startTime)); // 显然,使用缓冲区比不使用缓冲区耗时少了很多
// 类比理解:古代欧洲人写字,拿着棍子一次蘸一滴墨;古代中国人写字,拿着毛笔一次蘸一管墨(容器思想)
} /**
* 字节流一次读写一个字节(耗时:38080)
* @param src
* @param target
* @throws IOException
*/
public static void method1(String src, String target) throws IOException {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(target); int ch = 0; while ((ch = fis.read()) != -1) {
fos.write(ch);
fos.flush();
} fos.close();
fis.close();
} /**
* 字节流一次读写一个字节数组(字节数组作为缓冲区)(耗时:78)
* @param src
* @param target
* @throws IOException
*/
public static void method2(String src, String target) throws IOException {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(target); byte[] arr = new byte[1024];
int length = 0; while ((length = fis.read(arr)) != -1) {
fos.write(arr, 0, length);
fos.flush();
} fos.close();
fis.close();
} /**
* 缓冲字节流一次读写一个字节(耗时:23922)
* @param src
* @param target
* @throws IOException
*/
public static void method3(String src, String target) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target)); int ch = 0; while ((ch = bis.read()) != -1) {
bos.write(ch);
bos.flush();
} bos.close();
bis.close();
} /**
* 缓冲字节流一次读写一个字节数组(字节数组作为缓冲区)(耗时:78)
* @param src
* @param target
* @throws IOException
*/
public static void method4(String src, String target) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target)); byte[] arr = new byte[1024];
int length = 0; while ((length = bis.read(arr)) != -1) {
bos.write(arr, 0, length);
bos.flush();
} bos.close();
bis.close();
}
}
 package cn.temptation;

 import java.io.UnsupportedEncodingException;
import java.util.Arrays; public class Sample28 {
public static void main(String[] args) throws UnsupportedEncodingException {
/*
* 字节流操作英文数字字符很方便,但是对于非英文数字字符(比如:中文等)不够方便,所以Java中提供了字符流
*
* 字符流 = 字节流 + 编码表(字符集)
*
* 常见编码表(字符集):
* 1、ASCII:美国标准信息交换码
* 2、ISO8859-1:拉丁码表、欧洲码表
* 3、GB2312:中国的中文编码表
* 4、GBK:中国的中文编码表的升级
* 5、BIG-5:通行于香港、台湾地区的繁体字编码表
* 6、Unicode:国际标准码,所有的文字都使用2个字节表示
* 7、UTF-8:最多用3个字节表示一个字符(兼容最多的文字内容)
*
* String字符串类的编码和解码:
* 编码:字符串----->字节数组
* 解码:字节数组----->字符串
*
* String类的常用成员方法:
* 1、byte[] getBytes(Charset charset):使用给定的 charset 将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。
*
* String类的构造函数:
* 1、String(byte[] bytes, Charset charset):通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。
*/ String str = "中国"; // 编码
byte[] arr = str.getBytes("GBK");
System.out.println(Arrays.toString(arr)); // [-42, -48, -71, -6] // 解码
String result = new String(arr, "GBK");
// String result = new String(arr, "UTF-8");
System.out.println(result); // 中国 // 注意:编码和解码使用相同的编码表(字符集)
}
}
 package cn.temptation;

 import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter; public class Sample29 {
public static void main(String[] args) throws IOException {
/*
* 类 Writer:用于写入字符流的抽象类。
*
* Writer类的常用成员方法:
* 1、abstract void close():关闭此流,但要先刷新它。
* 2、abstract void flush():刷新该流的缓冲。
* 3、void write(String str):写入字符串。
*
* 类 OutputStreamWriter:OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的charset将要写入流中的字符编码成字节。
*
* OutputStreamWriter类的构造函数:
* 1、OutputStreamWriter(OutputStream out):创建使用默认字符编码的 OutputStreamWriter。
* 2、OutputStreamWriter(OutputStream out, String charsetName):创建使用指定字符集的 OutputStreamWriter。
*/
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\test01.txt")); osw.write("可以直接写出中文了!"); osw.flush();
osw.close();
}
}
 package cn.temptation;

 import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader; public class Sample30 {
public static void main(String[] args) throws IOException {
/*
* 类 Reader:用于读取字符流的抽象类。
*
* Reader类的常用成员方法:
* 1、abstract void close():关闭该流并释放与之关联的所有资源。
* 2、int read():读取单个字符。
*
* 类 InputStreamReader:字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。
*
* InputStreamReader类的构造函数:
* InputStreamReader(InputStream in):创建一个使用默认字符集的 InputStreamReader。
* InputStreamReader(InputStream in, String charsetName):创建使用指定字符集的 InputStreamReader。
*
* InputStreamReader类的常用成员方法:
* 1、int read():读取单个字符。
* 返回:读取的字符,如果已到达流的末尾,则返回 -1
*/ InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\test01.txt"), "UTF-8"); int ch = 0; while ((ch = isr.read()) != -1) {
System.out.println((char)ch);
} isr.close();
}
}
 package cn.temptation;

 import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter; public class Sample31 {
public static void main(String[] args) throws IOException {
// 需求:使用字符流实现从test01.txt读取内容,写出到test02.txt中 InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\test01.txt"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\test02.txt")); // 写法1、一次读写一个字符
// int ch = 0;
//
// while ((ch = isr.read()) != -1) {
// osw.write(ch);
// osw.flush();
// } // 写法2
char[] arr = new char[1024];
int length = 0; while ((length = isr.read(arr)) != -1) {
osw.write(arr, 0, length);
osw.flush();
} osw.close();
isr.close();
}
}
 package cn.temptation;

 import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; public class Sample32 {
public static void main(String[] args) throws IOException {
/*
* 因为InputStreamReader和OutputStreamWriter类名太长,所以Java语言提供了简化形式:FileReader和FileWriter
*
* FileReader类:用来读取字符文件的便捷类。InputStreamReader类的子类
*
* FileReader类的构造函数:
* FileReader(String fileName):在给定从中读取数据的文件名的情况下创建一个新 FileReader。
*
*
* FileWriter类:用来写入字符文件的便捷类。OutputStreamWriter类的子类
*
* FileWriter类的构造函数:
* FileWriter(String fileName):根据给定的文件名构造一个 FileWriter 对象。
*/ // 需求:使用简化字符流实现从test01.txt读取内容,写出到test02.txt中
FileReader reader = new FileReader("D:\\test01.txt");
FileWriter writer = new FileWriter("D:\\test02.txt"); char[] arr = new char[1024];
int length = 0; while ((length = reader.read(arr)) != -1) {
writer.write(arr, 0, length);
writer.flush();
} writer.close();
reader.close();
}
}
 package cn.temptation;

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; public class Sample33 {
public static void main(String[] args) throws IOException {
/*
* 类 BufferedReader:从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
*
* BufferedReader类的构造函数:
* BufferedReader(Reader in):创建一个使用默认大小输入缓冲区的缓冲字符输入流。
*
*
* 类 BufferedWriter:将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
*
* BufferedWriter类的构造函数:
* BufferedWriter(Writer out):创建一个使用默认大小输出缓冲区的缓冲字符输出流。
*/ // 需求:使用缓冲字符流实现从test01.txt读取内容,写出到test02.txt中
BufferedReader reader = new BufferedReader(new FileReader("D:\\test01.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\test02.txt")); char[] arr = new char[1024];
int length = 0; while ((length = reader.read(arr)) != -1) {
writer.write(arr, 0, length);
writer.flush();
} writer.close();
reader.close();
}
}
 package cn.temptation;

 import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random; public class Sample34 {
public static void main(String[] args) throws IOException {
// 需求:使用IO,制作一个抽奖程序(在文本文件中存储一系列人名,实现随机抽取一个) // 思路:
// 1、从文本文件中一行一行的读取
// 2、读取出来的名字存储在一个内存容器中
// 3、随机出一个数字,对应容器中的元素 /*
* BufferedReader类的常用成员方法:
* 1、String readLine():读取一个文本行。
* 返回:包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
*/ // 1、从文本文件中一行一行的读取
BufferedReader reader = new BufferedReader(new FileReader("人员名单.txt")); // 2、创建一个内存容器
Collection<String> collection = new ArrayList<>(); String temp = null; while ((temp = reader.readLine()) != null) {
collection.add(temp);
} reader.close(); // 3、随机产生一个数字
/*
* 类 Random:此类的实例用于生成伪随机数流。
*
* Random类的常用成员方法:
* int nextInt(int n):返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。
*/
Random random = new Random();
int index = random.nextInt(collection.size()); String result = ((ArrayList<String>)collection).get(index); System.out.println("恭喜" + result + "中奖啦!");
}
}
 package cn.temptation;

 import java.io.FileNotFoundException;
import java.io.PrintWriter; public class Sample35 {
public static void main(String[] args) throws FileNotFoundException {
/*
* 类 PrintWriter:向文本输出流打印对象的格式化表示形式。
*
* 打印流的特点:
* 1、只能操作目标,不能操作源头
* 2、操作可以是任意类型的数据
* 3、可以设置自动刷新
* 4、可以操作文本流
*/ PrintWriter writer = new PrintWriter("D:\\test01.txt"); writer.write("中国");
writer.write("美国");
writer.write("日本"); writer.flush();
writer.close();
}
}
 package cn.temptation;

 import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter; public class Sample36 {
public static void main(String[] args) throws IOException {
/*
* PrintWriter类的构造函数:
* 1、PrintWriter(OutputStream out, boolean autoFlush):通过现有的 OutputStream 创建新的 PrintWriter。
* 参数:out - 输出流;autoFlush - boolean 变量;如果为 true,则 println、printf 或 format 方法将刷新输出缓冲区
*
* PrintWriter类的常用成员方法:
* 1、void print(String s):打印字符串
* 2、void println(String x):打印 String,然后终止该行。
*/ PrintWriter writer = new PrintWriter(new FileWriter("D:\\test01.txt"), true); writer.println("中国");
writer.println("美国");
writer.println("日本"); writer.close();
}
}
 package cn.temptation;

 import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter; public class Sample37 {
public static void main(String[] args) throws IOException {
// 需求:使用字符缓冲流 和 字符打印流 从test01.txt文件中读取内容,写出到test02.txt文件中 BufferedReader reader = new BufferedReader(new FileReader("D:\\test01.txt"));
PrintWriter writer = new PrintWriter(new FileWriter("D:\\test02.txt")); String temp = null; while ((temp = reader.readLine()) != null) {
writer.println(temp);
writer.flush();
} writer.close();
reader.close();
}
}
 package cn.temptation;

 import java.io.PrintStream;
import java.util.Scanner; public class Sample38 {
public static void main(String[] args) {
/*
* 标准输入流:static InputStream in “标准”输入流。
* 标准输出流:static PrintStream out “标准”输出流。
*
* System类的字段:in 和 out
* 代表了系统标准的输入和输出设备,默认输入设备是键盘,输出设备是显示器
*/ Scanner input = new Scanner(System.in);
input.close(); System.out.println("temptation");
// 上句语句等价于
PrintStream ps = System.out;
ps.println("temptation");
}
}
 package cn.temptation;

 import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream; public class Sample39 {
public static void main(String[] args) throws IOException {
/*
* 字节混合流
* 类 SequenceInputStream:表示其他输入流的逻辑串联。
* 它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,
* 接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
*
* SequenceInputStream类的常用构造函数:
* SequenceInputStream(InputStream s1, InputStream s2):通过记住这两个参数来初始化新创建的 SequenceInputStream(将按顺序读取这两个参数,先读取 s1,然后读取 s2),以提供从此 SequenceInputStream 读取的字节。
*/ InputStream is1 = new FileInputStream("D:\\test01.txt");
InputStream is2 = new FileInputStream("D:\\test02.txt"); SequenceInputStream sis = new SequenceInputStream(is1, is2);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\test03.txt")); byte[] arr = new byte[1024];
int length = 0; while ((length = sis.read(arr)) != -1) {
bos.write(arr, 0, length);
bos.flush();
} bos.close();
sis.close();
is1.close();
is2.close();
}
}
 package cn.temptation;

 import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; public class Sample40 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
/*
* 序列化流 和 反序列化流
* 序列化流:把对象按照流的形式存储在资源中或在网络中传输
* 反序列化流:从资源中或在网络中传输把数据流还原为对象
*
* 类 ObjectOutputStream:将Java对象的基本数据类型和图形写入 OutputStream。
* 可以使用 ObjectInputStream读取(重构)对象。通过在流中使用文件可以实现对象的持久存储。
* 如果流是网络套接字流,则可以在另一台主机上或另一个进程中重构对象。
*
* ObjectOutputStream类的常用成员方法:
* void writeObject(Object obj):将指定的对象写入 ObjectOutputStream。
*
*
* 类 ObjectInputStream:对以前使用 ObjectOutputStream写入的基本数据和对象进行反序列化。
*
* ObjectInputStream类的常用成员方法:
* Object readObject():从 ObjectInputStream 读取对象。
*
* 接口 Serializable:类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。
* 可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,仅用于标识可序列化的语义。
*
*/ // write();
read();
} /**
* 序列化对象
* @throws IOException
*/
public static void write() throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\student.txt")); oos.writeObject(new Student("张三", 18)); oos.flush(); oos.close();
} /**
* 反序列化对象
* @throws IOException
* @throws ClassNotFoundException
*/
public static void read() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\student.txt")); Object object = ois.readObject(); ois.close(); System.out.println(object);
System.out.println("姓名为:" + ((Student)object).getStudentName() + ",年龄为:" + ((Student)object).getStudentAge());
}
}
 package cn.temptation;

 import java.io.Serializable;

 // 下句执行异常:java.io.NotSerializableException: cn.temptation.Student,因为没有实现Serializable接口
//public class Student {
public class Student implements Serializable {
private static final long serialVersionUID = 1L; // 成员变量
private String studentName;
private Integer studentAge; // 构造函数
public Student() {
super();
} public Student(String studentName, Integer studentAge) {
super();
this.studentName = studentName;
this.studentAge = studentAge;
} // 成员方法
public String getStudentName() {
return studentName;
} public void setStudentName(String studentName) {
this.studentName = studentName;
} public Integer getStudentAge() {
return studentAge;
} public void setStudentAge(Integer studentAge) {
this.studentAge = studentAge;
} }

【原】Java学习笔记033 - IO的更多相关文章

  1. java学习笔记之IO编程—内存流、管道流、随机流

    1.内存操作流 之前学习的IO操作输入和输出都是从文件中来的,当然,也可以将输入和输出的位置设置在内存上,这就需要用到内存操作流,java提供两类内存操作流 字节内存操作流:ByteArrayOutp ...

  2. Java学习笔记之——IO

    一. IO IO读写 流分类: 按照方向:输入流(读),输出流(写) 按照数据单位:字节流(传输时以字节为单位),字符流(传输时以字符为单位) 按照功能:节点流,过滤流 四个抽象类: InputStr ...

  3. Java学习笔记-10.io流

    1.输入流,只能从中读取数据,而不能向其写出数据.输出流,只能想起写入字节数据,而不能从中读取. 2.InputStream的类型有: ByteArrayInputStream 包含一个内存缓冲区,字 ...

  4. java学习笔记之IO编程—字节流和字符流

    1. 流的基本概念 在java.io包里面File类是唯一一个与文件本身有关的程序处理类,但是File只能够操作文件本身而不能操作文件的内容,或者说在实际的开发之中IO操作的核心意义在于:输入与输出操 ...

  5. java学习笔记之IO编程—对象序列化

    对象序列化就是将内存中保存的对象以二进制数据流的形式进行处理,可以实现对象的保存或网络传输. 并不是所有的对象都可以被序列化,如果要序列化的对象,那么对象所在的类一定要实现java.io.Serial ...

  6. java学习笔记之IO编程—打印流和BufferedReader

    1.打印流(PrintWriter) 想要通过程序实现内容输出,其核心一定是要依靠OutputStream类,但是OutputStream类有一个最大缺点,就是这个类中的输出操作功能有限,所有的数据一 ...

  7. java学习笔记之IO编程—目录和文件的拷贝

    进行文件或目录的拷贝时,要先判断处理对象是文件还是目录,如果是文件则直接拷贝,如果是目录还需要拷贝它的子目录及其文件,这就需要递归处理了 import java.io.*; class FileUti ...

  8. java学习笔记之IO编程—File文件操作类

    1. File类说明 在Java语言里面提供有对于文件操作系统操作的支持,而这个支持就在java.io.File类中进行了定义,也就是说在整个java.io包里面,File类是唯一一个与文件本身操作( ...

  9. Java学习笔记--文件IO

    简介 对于任何程序设计语言,输入和输出(Input\Output)都是系统非常核心的功能,程序运行需要数据,而数据的获取往往需要跟外部系统进行通信,外部系统可能是文件.数据库.其他程序.网络.IO设备 ...

随机推荐

  1. 【原】Oracle EBS 11无法打开Form及Form显示乱码的解决

    问题:Oracle EBS 11无法打开Form及Form显示乱码 解决: 1.尝试使用jre1.5或1.6安装目录下jre/bin/server目录里的jvm.dll替换JInitiator安装目录 ...

  2. MongoDB 4.0 开发环境搭建集群

    环境准备 Liunx 服务器一台 以下示例为单机版安装集群, 没有分片 MongoDB 安装 1.下载 MongoDB tgz 安装包: 可以从下载中心下载: https://www.mongodb. ...

  3. Java核心基础学习(一)--- 2019年1月

    1.对比Exception和Error,运行时异常与一般异常 Exception 和 Error 都继承了 Throwable 类,在 Java 中只有 Throwable 类才能 thorw(抛出) ...

  4. java调用python程序以及向python程序传递参数

    在做项目的时候,经常会碰到这个问题,主要程序是用java写的,有些功能使用python写的,整个项目需要把java代码和python代码进行整合,在一个项目里面运行,这就涉及到java调用python ...

  5. PHP内核之旅-6.垃圾回收机制

    回收PHP 内核之旅系列 PHP内核之旅-1.生命周期 PHP内核之旅-2.SAPI中的Cli PHP内核之旅-3.变量 PHP内核之旅-4.字符串 PHP内核之旅-5.强大的数组 PHP内核之旅-6 ...

  6. 【重学计算机】计组D1章:计算机系统概论

    1.冯诺依曼计算机组成 主机(cpu+内存),外设(输入设备+输出设备+外存),总线(地址总线+数据总线+控制总线) 2.计算机层次结构 应用程序-高级语言-汇编语言-操作系统-指令集架构层-微代码层 ...

  7. Ubuntu16.04 部署配置GO语言开发环境 & 注意事项

    1. 安装GO 安装go语言包: $ curl -O https://storage.googleapis.com/golang/go1.10.1.linux-amd64.tar.gz   下载完成后 ...

  8. NotificationSetUtilDemo【判断APP通知栏权限是否开启,以及如何跳转到应用程序设置界面】

    前言 当APP有推送功能时,需要判断当前app在手机中是否开启了允许消息推送,否则即使添加了推送代码仍然收不到通知. 效果图 oppo上的效果: 使用步骤 一.项目组织结构图 注意事项: 1.  导入 ...

  9. 【设计模式+原型理解】第三章:javascript五种继承父类方式

    [前言] 我们都知道,面向对象(类)的三大特征:封装.继承.多态 继承:子类继承父类的私有属性和公有方法 封装:把相同的代码写在一个函数中 多态: ->重载:JS严格意义上是没有重载,但可以通过 ...

  10. Springboot 系列(九)使用 Spring JDBC 和 Druid 数据源监控

    前言 作为一名 Java 开发者,相信对 JDBC(Java Data Base Connectivity)是不会陌生的,JDBC作为 Java 基础内容,它提供了一种基准,据此可以构建更高级的工具和 ...