Java基础回顾系列-第七天-高级编程之IO
文件操作
// Serializable:可序列化
// Comparable:可以进行排序
public class File extends Object implements Serializable, Comparable<File> {
// 通过将给定路径名字符串转换成抽象路径名来创建一个新 File 实例。
public File(String pathname) {}
// 根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
public File(String parent, String child) {}
// 系统分隔符 "d:" + File.separator +".aaa.txt";
public static final String separator = "" + separatorChar;
}
//1.创建文件夹
//import java.io.*;
File myFolderPath = new File(str1);
try {
if (!myFolderPath.exists()) {
myFolderPath.mkdir();
}
}
catch (Exception e) {
System.out.println("新建目录操作出错");
e.printStackTrace();
}
//2.创建文件
//import java.io.*;
File myFilePath = new File(str1);
try {
if (!myFilePath.exists()) {
myFilePath.createNewFile();
}
FileWriter resultFile = new FileWriter(myFilePath);
PrintWriter myFile = new PrintWriter(resultFile);
myFile.println(str2);
resultFile.close();
}
catch (Exception e) {
System.out.println("新建文件操作出错");
e.printStackTrace();
}
//3.删除文件
//import java.io.*;
File myDelFile = new File(str1);
try {
myDelFile.delete();
}
catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
}
//4.删除文件夹
//import java.io.*;
File delFolderPath = new File(str1);
try {
delFolderPath.delete(); //删除空文件夹
}
catch (Exception e) {
System.out.println("删除文件夹操作出错");
e.printStackTrace();
}
//5.删除一个文件下夹所有的文件夹
//import java.io.*;
File delfile=new File(str1);
File[] files=delfile.listFiles();
for(int i=0;i<files.length;i++){
if(files[i].isDirectory()){
files[i].delete();
}
}
//6.清空文件夹
//import java.io.*;
File delfilefolder=new File(str1);
try {
if (!delfilefolder.exists()) {
delfilefolder.delete();
}
delfilefolder.mkdir();
}
catch (Exception e) {
System.out.println("清空目录操作出错");
e.printStackTrace();
}
//7.读取文件
//import java.io.*;
// 逐行读取数据
FileReader fr = new FileReader(str1);
BufferedReader br = new BufferedReader(fr);
String str2 = br.readLine();
while (str2 != null) {
str3
str2 = br.readLine();
}
br.close();
fr.close();
//8.写入文件
//import java.io.*;
// 将数据写入文件
try {
FileWriter fw = new FileWriter(str1);
fw.write(str2);
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
//9.写入随机文件
//import java.io.*;
try {
RandomAcessFile logFile=new RandomAcessFile(str1,"rw");
long lg=logFile.length();
logFile.seek(str2);
logFile.writeByte(str3);
}catch(IOException ioe){
System.out.println("无法写入文件:"+ioe.getMessage());
}
//10.读取文件属性
//import java.io.*;
// 文件属性的取得
File f = new File(str1);
if (af.exists()) {
System.out.println(f.getName() + "的属性如下: 文件长度为:" + f.length());
System.out.println(f.isFile() ? "是文件" : "不是文件");
System.out.println(f.isDirectory() ? "是目录" : "不是目录");
System.out.println(f.canRead() ? "可读取" : "不");
System.out.println(f.canWrite() ? "是隐藏文件" : "");
System.out.println("文件夹的最后修改日期为:" + new Date(f.lastModified()));
} else {
System.out.println(f.getName() + "的属性如下:");
System.out.println(f.isFile() ? "是文件" : "不是文件");
System.out.println(f.isDirectory() ? "是目录" : "不是目录");
System.out.println(f.canRead() ? "可读取" : "不");
System.out.println(f.canWrite() ? "是隐藏文件" : "");
System.out.println("文件的最后修改日期为:" + new Date(f.lastModified()));
}
if(f.canRead()){
str2
}
if(f.canWrite()){
str3
}
//11.写入属性
//import java.io.*;
File filereadonly=new File(str1);
try {
boolean b=filereadonly.setReadOnly();
}
catch (Exception e) {
System.out.println("拒绝写访问:"+e.printStackTrace());
}
//12.枚举一个文件夹中的所有文件
//import java.io.*;
//import java.util.*;
LinkedList<String> folderList = new LinkedList<String>();
folderList.add(str1);
while (folderList.size() > 0) {
File file = new File(folderList.peek());
folderList.removeLast();
File[] files = file.listFiles();
ArrayList<File> fileList = new ArrayList<File>();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
folderList.add(files[i].getPath());
} else {
fileList.add(files[i]);
}
}
for (File f : fileList) {
str2=f.getAbsoluteFile();
str3
}
}
//13.复制文件夹
//import java.io.*;
//import java.util.*;
LinkedList<String> folderList = new LinkedList<String>();
folderList.add(str1);
LinkedList<String> folderList2 = new LinkedList<String>();
folderList2.add(str2+ str1.substring(str1.lastIndexOf("\\")));
while (folderList.size() > 0) {
(new File(folderList2.peek())).mkdirs(); // 如果文件夹不存在 则建立新文件夹
File folders = new File(folderList.peek());
String[] file = folders.list();
File temp = null;
try {
for (int i = 0; i < file.length; i++) {
if (folderList.peek().endsWith(File.separator)) {
temp = new File(folderList.peek() + File.separator
+ file[i]);
} else {
temp = new File(folderList.peek() + File.separator + file[i]);
}
if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(
folderList2.peek() + File.separator + (temp.getName()).toString());
byte[] b = new byte[5120];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if (temp.isDirectory()) {// 如果是子文件夹
for (File f : temp.listFiles()) {
if (f.isDirectory()) {
folderList.add(f.getPath());
folderList2.add(folderList2.peek()
+ File.separator + f.getName());
}
}
}
}
} catch (Exception e) {
//System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
folderList.removeFirst();
folderList2.removeFirst();
}
//14.复制一个文件夹下所有的文件夹到另一个文件夹下
//import java.io.*;
//import java.util.*;
File copyfolders=new File(str1);
File[] copyfoldersList=copyfolders.listFiles();
for(int k=0;k<copyfoldersList.length;k++){
if(copyfoldersList[k].isDirectory()){
ArrayList<String>folderList=new ArrayList<String>();
folderList.add(copyfoldersList[k].getPath());
ArrayList<String>folderList2=new ArrayList<String>();
folderList2.add(str2+"/"+copyfoldersList[k].getName());
for(int j=0;j<folderList.length;j++){
(new File(folderList2.get(j))).mkdirs(); //如果文件夹不存在 则建立新文件夹
File folders=new File(folderList.get(j));
String[] file=folders.list();
File temp=null;
try {
for (int i = 0; i < file.length; i++) {
if(folderList.get(j).endsWith(File.separator)){
temp=new File(folderList.get(j)+"/"+file[i]);
} else {
temp=new File(folderList.get(j)+"/"+File.separator+file[i]);
}
FileInputStream input = new FileInputStream(temp);
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(folderList2.get(j) + "/" + (temp.getName()).toString());
byte[] b = new byte[5120];
int len;
while ( (len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夹
folderList.add(folderList.get(j)+"/"+file[i]);
folderList2.add(folderList2.get(j)+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
}
}
//15.移动文件夹
//import java.io.*;
//import java.util.*;
LinkedList<String> folderList = new LinkedList<String>();
folderList.add(str1);
LinkedList<String> folderList2 = new LinkedList<String>();
folderList2.add(str2 + str1.substring(str1.lastIndexOf("\\")));
while (folderList.size() > 0) {
(new File(folderList2.peek())).mkdirs(); // 如果文件夹不存在 则建立新文件夹
File folders = new File(folderList.peek());
String[] file = folders.list();
File temp = null;
try {
for (int i = 0; i < file.length; i++) {
if (folderList.peek().endsWith(File.separator)) {
temp = new File(folderList.peek() + File.separator + file[i]);
} else {
temp = new File(folderList.peek() + File.separator + file[i]);
}
if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(
folderList2.peek() + File.separator + (temp.getName()).toString());
byte[] b = new byte[5120];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
if (!temp.delete())
System.out.println("删除单个文件操作出错!");
}
if (temp.isDirectory()) {// 如果是子文件夹
for (File f : temp.listFiles()) {
if (f.isDirectory()) {
folderList.add(f.getPath());
folderList2.add(folderList2.peek() + File.separator + f.getName());
}
}
}
}
} catch (Exception e) {
// System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
folderList.removeFirst();
folderList2.removeFirst();
}
File f = new File(str1);
if (!f.delete()) {
for (File file : f.listFiles()) {
if (file.list().length == 0) {
System.out.println(file.getPath());
file.delete();
}
}
}
//16.移动一个文件夹下所有的文件夹到另一个目录下
//import java.io.*;
//import java.util.*;
File movefolders=new File(str1);
File[] movefoldersList=movefolders.listFiles();
for(int k=0;k<movefoldersList.length;k++){
if(movefoldersList[k].isDirectory()){
ArrayList<String>folderList=new ArrayList<String>();
folderList.add(movefoldersList[k].getPath());
ArrayList<String>folderList2=new ArrayList<String>();
folderList2.add(str2+"/"+movefoldersList[k].getName());
for(int j=0;j<folderList.length;j++){
(new File(folderList2.get(j))).mkdirs(); //如果文件夹不存在 则建立新文件夹
File folders=new File(folderList.get(j));
String[] file=folders.list();
File temp=null;
try {
for (int i = 0; i < file.length; i++) {
if(folderList.get(j).endsWith(File.separator)){
temp=new File(folderList.get(j)+"/"+file[i]);
}
else{
temp=new File(folderList.get(j)+"/"+File.separator+file[i]);
}
FileInputStream input = new FileInputStream(temp);
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(folderList2.get(j) + "/" + (temp.getName()).toString());
byte[] b = new byte[5120];
int len;
while ( (len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
temp.delete();
}
if(temp.isDirectory()){//如果是子文件夹
folderList.add(folderList.get(j)+"/"+file[i]);
folderList2.add(folderList2.get(j)+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
movefoldersList[k].delete();
}
}
//17.以一个文件夹的框架在另一个目录创建文件夹和空文件
//import java.io.*;
//import java.util.*;
boolean b=false;//不创建空文件
ArrayList<String>folderList=new ArrayList<String>();
folderList.add(str1);
ArrayList<String>folderList2=new ArrayList<String>();
folderList2.add(str2);
for(int j=0;j<folderList.length;j++){
(new File(folderList2.get(j))).mkdirs(); //如果文件夹不存在 则建立新文件夹
File folders=new File(folderList.get(j));
String[] file=folders.list();
File temp=null;
try {
for (int i = 0; i < file.length; i++) {
if(folderList.get(j).endsWith(File.separator)){
temp=new File(folderList.get(j)+"/"+file[i]);
}
else{
temp=new File(folderList.get(j)+"/"+File.separator+file[i]);
}
FileInputStream input = new FileInputStream(temp);
if(temp.isFile()){
if (b) temp.createNewFile();
}
if(temp.isDirectory()){//如果是子文件夹
folderList.add(folderList.get(j)+"/"+file[i]);
folderList2.add(folderList2.get(j)+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
//18.复制文件
//import java.io.*;
int bytesum = 0;
int byteread = 0;
File oldfile = new File(str1);
try {
if (oldfile.exists()) { //文件存在时
FileInputStream inStream = new FileInputStream(oldfile); //读入原文件
FileOutputStream fs = new FileOutputStream(new File(str2,oldfile.getName()));
byte[] buffer = new byte[5120];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
//19.复制一个文件夹下所有的文件到另一个目录
//import java.io.*;
File copyfiles=new File(str1);
File[] files=copyfiles.listFiles();
for(int i=0;i<files.length;i++){
if(!files[i].isDirectory()){
int bytesum = 0;
int byteread = 0;
try {
InputStream inStream = new FileInputStream(files[i]); //读入原文件
FileOutputStream fs = new FileOutputStream(new File(str2,files[i].getName());
byte[] buffer = new byte[5120];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
}
//提取扩展名
String str2=str1.substring(str1.lastIndexOf(".")+1);
// 重命名
public boolean renameTo(File dest);
字节流与字符流
一个流被定义为一个数据序列。输入流用于从源读取数据,输出流用于向目标写数据。
下图是一个描述输入流和输出流的类层次图。
字节流:InputStream
OutputStream
字符流:Writer
Reader
OutputStream字节输出流
结构:
// Closeable: 可自动关闭
// Flushable: 刷新
public abstract class OutputStream extends Object implements Closeable, Flushable {
// 关闭此文件输出流并释放与此流有关的所有系统资源。抛出IOException异常。
public void close() throws IOException
// 刷新此输出流并强制写出任何缓冲的输出字节
public void flush() throws IOException
// 输出单个字节数据
public abstract void write(int b) throws IOException
// 输出一组字节数据
public void write(byte[] b) throws IOException
// 输出部分字节数据
public void write(byte[] b, int off, int len) throws IOException
}
FileOutputStream
public FileOutputStream(File file) throws FileNotFoundException
// append:追加方式
public FileOutputStream(File file, boolean append) throws FileNotFoundException
public FileOutputStream(String name) throws FileNotFoundException
public FileOutputStream(String name, boolean append) throws FileNotFoundException
package javase.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class JavaAPIDemo {
public static void main(String[] args) {
File file = new File("d:" + File.separator + "a.txt");
// 使用了AutoCloseable,自动关闭释放
try (OutputStream outputStream = new FileOutputStream(file)) {
outputStream.write("写入文件成功".getBytes());
}catch (IOException e) {
e.printStackTrace();
}
}
}
InputStream字节输入流
结构:
// Closeable: 可自动关闭
public abstract class InputStream extends Object implements Closeable {
// 读取单个字节数据
public abstract int read() throws IOException
// 读取一组字节数据
public int read(byte[] b) throws IOException
// 读取一组字节部分数据
public int read(byte[] b, int off, int len) throws IOException
// 关闭此文件输入流并释放与此流有关的所有系统资源。抛出IOException异常。
public void close() throws IOException
}
FileInputStream
public FileInputStream(File file) throws FileNotFoundException
public FileInputStream(String name) throws FileNotFoundException
package javase.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class JavaAPIDemo {
public static void main(String[] args) {
File file = new File("d:" + File.separator + "a.txt");
// 使用了AutoCloseable,自动关闭释放
try (InputStream inputStream = new FileInputStream(file)) {
// 输入流中的总字节数
int available = inputStream.available();
byte[] bt = new byte[1024];
// 读取一组字节数据,返回读取到的字节数
int read = inputStream.read(bt);
// 将读取到的字节转换为字符串
System.out.println(new String(bt, 0, read));
}catch (IOException e) {
e.printStackTrace();
}
}
}
Writer字符输出流
结构:
// Appendable: 可追加
// Closeable: 可自动关闭
// Flushable: 刷新
public abstract class Writer extends Object implements Appendable, Closeable, Flushable {
public Writer append(char c) throws IOException
public Writer append(CharSequence csq) throws IOException
public Writer append(CharSequence csq, int start, int end) throws IOException
public void write(char[] cbuf) throws IOException
public void write(String str) throws IOException
public void write(String str, int off, int len) throws IOException
}
FileWriter
package javase.util;
import java.io.*;
public class JavaAPIDemo {
public static void main(String[] args) throws IOException {
File file = new File("d:" + File.separator + "a.txt");
if (! file.exists()) {
file.createNewFile();
}
// 自动关闭
try (Writer writer = new FileWriter(file)) {
// 可以利用字符串完成
writer.write("数据写入成功");
}catch (IOException e) {
e.printStackTrace();
}
}
}
Reader字符输入流
// Readable:
// Closeable: 可自动关闭
public abstract class Reader extends Object implements Readable, Closeable {、
// 判断此流是否可以读取
public boolean ready() throws IOException
// 读取
public int read() throws IOException
// 读取一组字符流
public int read(char[] cbuf) throws IOException
public abstract int read(char[] cbuf, int off, int len) throws IOException
public int read(CharBuffer target) throws IOException
// 重置流
public void reset() throws IOException
// 跳过指定字符
public long skip(long n) throws IOException
}
package javase.util;
import java.io.*;
public class JavaAPIDemo {
public static void main(String[] args) throws IOException {
File file = new File("d:" + File.separator + "a.txt");
// 自动关闭
try (Reader reader = new FileReader(file)) {
char[] cbuf = new char[1024];
int read = reader.read(cbuf);
System.out.println(new String(cbuf, 0, read));
}catch (IOException e) {
e.printStackTrace();
}
}
}
字节流与字符流的区别
参考博文:字节流与字符流的区别
转换流
可以实现字节流和字符流操作的功能转换。
- InputStreamReader 类包含了一个底层输入流,可以从中读取原始字节。它根据指定的编码方式,将这些字节转换为Unicode字符。
- OutputStreamWriter 从运行的程序中接收Unicode字符,然后使用指定的编码方式将这些字符转换为字节,再将这些字节写入底层输出流中。
InputStreamReader
public class InputStreamReader extends Reader {
// 构造函数
public InputStreamReader(InputStream in) throws UnsupportedEncodingException
// 构造函数,指定字符集
public InputStreamReader(InputStream in, String charsetName) throws UnsupportedEncodingException
}
OutputStreamWriter
public class OutputStreamWriter extends Writer {
// 构造函数
public OutputStreamWriter(OutputStream out)
public OutputStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException
}
文件复制
package javase.util;
import java.io.*;
/**
* 文件复制
*/
public class JavaAPIDemo {
public static void main(String[] args) throws IOException {
// 需要复制的源文件
String srcFile = "d:" + File.separator + "a.txt";
// 需复制的目标文件
String desFile = "d:" + File.separator + "b.txt";
// 实现方式一:
// byte[] data = new byte[1024];
// // 自动关闭
// try (InputStream inputStream = new FileInputStream(srcFile);OutputStream outputStream = new FileOutputStream(desFile)){
// int len;
// while ((len = inputStream.read(data)) != -1) {
// outputStream.write(data, 0, len);
// }
// }
// 实现方式二:JDK1.9之后
// 自动关闭
try (InputStream inputStream = new FileInputStream(srcFile);OutputStream outputStream = new FileOutputStream(desFile)){
inputStream.transferTo(outputStream);
}
}
}
字符编码
GBK/GB2312: 国标编码,可以描述中文信息,其中GBK只描述简体中文;而GB2312可以描述简体中文以及繁体中文。
ISO8859-1: 国际通用编码,可以描述所有的字母信息,如果是象形文字则需要进行编码处理。
UNICODE编码: 采用十六进制的方式存储,可以描述所有的文字信息。缺陷:范围太大。
UTF编码: 象形文字部分采用十六进制编码,而普通的字母采用ISO8859-1编码,它的优势在于适合快速传输,节约带宽,也就成为了开发中首选的编码。主要使用UTF-8编码。
内存操作流(使用较少)
- 字节内存操作流:
ByteArrayInputStream
、ByteArrayOutputStream
- 字符内存操作流:
CharArrayReader
、CharArrayWriter
ByteArrayInputStream
public class ByteArrayInputStream extends InputStream {
// 构造函数:将一组字节保存到内存流
public ByteArrayInputStream(byte[] buf)
}
ByteArrayOutputStream
public class ByteArrayOutputStream extends OutputStream {
public byte[] toByteArray()
public String toString()
}
示例
package javase.util;
import java.io.*;
public class JavaAPIDemo {
public static void main(String[] args) throws IOException {
String string = "ABCDEF";
InputStream inputStream = new ByteArrayInputStream(string.getBytes());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 单个字节读取
int read;
while ((read = inputStream.read()) != -1) {
System.out.println("read = " + Character.toString(read));
outputStream.write(Character.valueOf((char)read));
}
System.out.println("outputStream = " + outputStream);
byte[] bytes = outputStream.toByteArray();
System.out.println(new String(bytes));
inputStream.close();
outputStream.close();
}
}
管道流
- 字节管道流:
PipedInputStream
、PipedOutputStream
- 字符管道流:
PipedReader
、PipedWriter
重要的连接:
PipedOutputStream
:public void connect(PipedInputStream snk) throws IOException
PipedWriter
:public void connect(PipedReader snk) throws IOException
RandomAccessFile随机读取
我们平常创建流对象关联文件,开始读文件或者写文件都是从头开始的,不能从中间开始,如果是开多线程下载一个文件我们之前学过的FileWriter或者FileReader等等都无法完成,而当前介绍的RandomAccessFile他就可以解决这个问题,因为它可以指定位置读,指定位置写的一个类,通常开发过程中,多用于多线程下载一个大文件。
RandomAccessFile的一个重要使用场景就是网络请求中的多线程下载及断点续传。
既可以读也可以写
可以指定位置读写
构造方法:其中参数 mode 的值可选 “r”:可读,“w” :可写,“rw”:可读性;
- public RandomAccessFile(File file, String mode) throws FileNotFoundException
- public RandomAccessFile(String name, String mode) throws FileNotFoundException
一共有4种模式:
- “r”: 以只读方式打开。调用结果对象的任何 write 方法都将导致抛出 IOException。
- “rw”: 打开以便读取和写入。
- “rws”: 打开以便读取和写入。相对于 “rw”,“rws” 还要求对“文件的内容”或“元数据”的每个更新都同步写入到基础存储设备。
- “rwd” : 打开以便读取和写入,相对于 “rw”,“rwd” 还要求对“文件的内容”的每个更新都同步写入到基础存储设备。
输入与输出支持
打印流(重要)
PrintStream
public class PrintStream extends FilterOutputStream implements Appendable, Closeable {
public PrintStream(OutputStream out)
public PrintStream(File file) throws FileNotFoundException
public PrintStream(String fileName) throws FileNotFoundException
}
PrintWriter(更推荐)
public class PrintWriter extends Writer {
public PrintWriter(OutputStream out)
public PrintWriter(OutputStream out, boolean autoFlush)
public PrintWriter(File file) throws FileNotFoundException
public PrintWriter(String fileName, Charset charset) throws IOException
public PrintWriter append(CharSequence csq)
// 格式化。与System.out.format()相同
public PrintWriter format(String format, Object... args)
public void println(String x)
public void write(char[] buf)
public void write(String s)
}
System类对IO的支持
public final class System extends Object {
// 标准输出
public static final PrintStream out;
// 错误输出
public static final PrintStream err;
// 标准输入(键盘)
public static final InputStream in
;
}
BufferedReader缓冲输入流
二者都可以从键盘接收数据,API文档解释,public BufferedReader(Reader in)方法创建使用默认大小的输入缓冲区的缓冲字符输入流。public Scanner(InputStream source) 构造一个新的扫描器,它产生从指定的输入流扫描的值,从流字节转换为字符使用基础平台的默认字符集。
public class BufferedReader extends Reader {
// 读取一行
public String readLine() throws IOException
}
Scanner扫描流
Scanner是为了替代BufferedReader而产生。该类的API处理更简单。
BufferedReader与Scanner比较
BufferedReader 是支持同步的,而 Scanner 不支持。如果我们处理多线程程序,BufferedReader 应当使用。
BufferedReader 相对于 Scanner 有足够大的缓冲区内存。
Scanner 有很少的缓冲区(1KB 字符缓冲)相对于 BufferedReader(8KB字节缓冲),但是这是绰绰有余的。
BufferedReader 相对于 Scanner 来说要快一点,因为 Scanner 对输入数据进行类解析,而 BufferedReader 只是简单地读取字符序列。
对象序列化
参考博文:Java基础学习总结——Java对象的序列化和反序列化[https://www.cnblogs.com/xdp-gacl/p/3777987.html]
序列化和反序列化的概念
把对象转换为字节序列的过程称为对象的序列化。
把字节序列恢复为对象的过程称为对象的反序列化。
对象的序列化主要有两种用途:
- 把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中;
- 在网络上传送对象的字节序列。
在很多应用中,需要对某些对象进行序列化,让它们离开内存空间,入住物理硬盘,以便长期保存。比如最常见的是Web服务器中的Session对象,当有 10万用户并发访问,就有可能出现10万个Session对象,内存可能吃不消,于是Web容器就会把一些seesion先序列化到硬盘中,等要用了,再把保存在硬盘中的对象还原到内存中。当两个进程在进行远程通信时,彼此可以发送各种类型的数据。无论是何种类型的数据,都会以二进制序列的形式在网络上传送。发送方需要把这个Java对象转换为字节序列,才能在网络上传送;接收方则需要把字节序列再恢复为Java对象。
JDK类库中的序列化API
1、序列化之后保存的是类的信息
2、被声明为transient的属性不会被序列化,这就是transient关键字的作用
3、被声明为static的属性不会被序列化,这个问题可以这么理解,序列化保存的是对象的状态,但是static修饰的变量是属于类的而不是属于变量的,因此序列化的时候不会序列化它
Serializable序列化标记
ObjectOutputStream对象输出流
ObjectInputStream对象输入流
transient关键字-手动指定序列化过程
Java中transient关键字的作用,简单地说,就是让某些被修饰的成员属性变量不被序列化,这一看好像很好理解,就是不被序列化,那么什么情况下,一个对象的某些字段不需要被序列化呢?如果有如下情况,可以考虑使用关键字transient修饰:
private transient Integer area;