JavaSE基础
一、File
1.1 概述
文件和目录路径名的抽象表示形式。
把文件管理系统中的字符串路径创建称为Java中的对象 具备了一些属性和方法,方便在程序中操作文件
1.2 创建对象
package com. javastudy ;
import java. io. File ;
public class TestIO01 {
public static void main ( String [ ] args) {
File file = new File ( "E:\\JavaStudy" ) ;
System . out. println ( file) ;
String pathname = "E:\\JavaStudy" ;
File file2 = new File ( pathname, "Day01" ) ;
System . out. println ( file2) ;
System . out. println ( file2. exists ( ) ) ;
File file3 = new File ( file, "Day01" ) ;
System . out. println ( file3) ;
System . out. println ( file3. exists ( ) ) ;
}
}
路径的写法
package com. javastudy ;
import java. io. File ;
public class TestIO02 {
public static void main ( String [ ] args) {
String pathName = "E:\\JavaStudy/Day01\\\\Java" ;
System . out. println ( ) ;
File file = new File ( pathName) ;
System . out. println ( file. getFreeSpace ( ) ) ;
System . out. println ( file. getTotalSpace ( ) ) ;
System . out. println ( file. exists ( ) ) ;
File file2 = new File ( "hello.txt" ) ;
System . out. println ( file2. exists ( ) ) ;
System . out. println ( file2. getAbsolutePath ( ) ) ;
}
}
1.3 常用方法
增删改
package com. javastudy ;
import java. io. File ;
import java. io. IOException ;
public class TestIO03 {
public static void main ( String [ ] args) throws IOException , InterruptedException {
File file = new File ( "E:\\JavaStudy\\Day01" ) ;
File file2 = new File ( file, "Day02" ) ;
System . out. println ( file2. exists ( ) ) ;
boolean b = file2. mkdir ( ) ;
System . out. println ( b) ;
File file3 = new File ( file, "Day03/笔记" ) ;
System . out. println ( file3. exists ( ) ) ;
b = file3. mkdirs ( ) ;
System . out. println ( b) ;
File file4 = new File ( file, "hehe.txt" ) ;
file4. createNewFile ( ) ;
System . out. println ( "10S后删除..." ) ;
Thread . sleep ( 10000 ) ;
System . out. println ( "OVER" ) ;
File file5 = new File ( file, "Day03" ) ;
b = file5. delete ( ) ;
System . out. println ( b) ;
File dest = new File ( file, "hehehe.txt" ) ;
b = file4. renameTo ( dest) ;
System . out. println ( b) ;
}
}
查询
package com. javastudy ;
import java. io. File ;
public class TestIO04 {
public static void main ( String [ ] args) {
File file = new File ( "E:\\JavaStudy\\Day01" ) ;
File file2 = new File ( file, "hehehe.txt" ) ;
System . out. println ( file2. exists ( ) ) ;
System . out. println ( file2. isDirectory ( ) ) ;
System . out. println ( file2. isFile ( ) ) ;
System . out. println ( file2. isHidden ( ) ) ;
System . out. println ( file2. getName ( ) ) ;
System . out. println ( file2. getParent ( ) ) ;
System . out. println ( file2. length ( ) ) ;
File [ ] files = file. listFiles ( ) ;
for ( File file3 : files) {
System . out. println ( file3) ;
}
}
}
二、文件过滤器
2.1 概述
2.2 案例
package com. javastudy ;
import java. io. File ;
import java. io. FileFilter ;
public class TestIO05 {
public static void main ( String [ ] args) {
File file = new File ( "E:\\JavaStudy\\Day01" ) ;
FileFilter filter = new FileFilter ( ) {
@Override
public boolean accept ( File filters) {
return filters. getName ( ) . endsWith ( ".docx" ) ;
}
} ;
File [ ] files = file. listFiles ( filter) ;
for ( File f : files) {
System . out. println ( f) ;
}
}
}
2.3 递归获取文件和文件夹【考题】
package com. javastudy ;
import java. io. File ;
public class TestFile02 {
public static void main ( String [ ] args) {
File file = new File ( "E:\\JavaStudy\\Day01" ) ;
getFiles ( file) ;
}
public static void getFiles ( File file) {
File [ ] files = file. listFiles ( ) ;
for ( File f : files) {
if ( f. isFile ( ) ) {
System . out. println ( "\t文件:" + f) ;
} else {
System . out. println ( "文件夹:" + f) ;
getFiles ( f) ;
}
}
}
}
三、字节输入流
3.1 概述
3.2 继承体系
InputStream
FileInputStream
3.3 FileInputStream
1、创建流对象,指定读取的文件
2、读取数据
read(byte[] b)
3、关闭流
package com. javastudy ;
import java. io. FileInputStream ;
import java. io. FileNotFoundException ;
import java. io. IOException ;
import java. util. Arrays ;
public class TestInputStream {
public static void main ( String [ ] args) throws IOException {
FileInputStream fis = new
FileInputStream ( "E:\\JavaStudy\\Day19\\src\\com\\javastudy\\hello.txt" ) ;
byte [ ] b = new byte [ 4 ] ;
int len = fis. read ( b) ;
System . out. println ( "读取内容的数量:" + len) ;
System . out. println ( "第一次读取的数据:" + Arrays . toString ( b) ) ;
len = fis. read ( b) ;
System . out. println ( "读取内容的数量:" + len) ;
System . out. println ( "第二次读取的数据:" + Arrays . toString ( b) ) ;
len = fis. read ( b) ;
System . out. println ( "读取内容的数量:" + len) ;
System . out. println ( "第三次读取的数据:" + Arrays . toString ( b) ) ;
len = fis. read ( b) ;
System . out. println ( "读取内容的数量:" + len) ;
System . out. println ( "第四次读取的数据:" + Arrays . toString ( b) ) ;
fis. close ( ) ;
}
}
四、字节输出流
4.1 概述
4.2 继承体系
OutputStream
FileOutputStream
文件输出流是用于将数据写入 File
或 FileDescriptor
的输出流。 FileOutputStream
用于写入诸如图像数据之类的原始字节的流。
4.3 FileOutputStream
1、创建流对象,指明数据写入的文件
2、写入数据
3、关闭流
package com. javastudy ;
import java. io. FileNotFoundException ;
import java. io. FileOutputStream ;
import java. io. IOException ;
public class TestOutPutStream {
public static void main ( String [ ] args) throws IOException {
FileOutputStream fop = new FileOutputStream ( "hello.txt" , true ) ;
byte [ ] b = new byte [ ] { 72 , 73 , 74 , 75 , 76 , 77 , 78 } ;
fop. write ( b, 3 , 4 ) ;
fop. close ( ) ;
}
}
4.4 复制文件
第一版
package com. javastudy ;
import java. io. FileInputStream ;
import java. io. FileOutputStream ;
import java. io. IOException ;
public class TestCopy03 {
public static void main ( String [ ] args) throws IOException {
long startTime = System . currentTimeMillis ( ) ;
FileInputStream fip = new FileInputStream ( "D:\\Hello.txt" ) ;
FileOutputStream fop = new FileOutputStream ( "E:\\Hello.txt" ) ;
int len = - 1 ;
while ( ( len = fip. read ( b) ) != - 1 ) {
fop. write ( b, 0 , len) ;
}
fip. close ( ) ;
fop. close ( ) ;
long endTime = System . currentTimeMillis ( ) ;
System . out. println ( endTime - startTime) ;
}
}
第二版【数组】
package com. javastudy ;
import java. io. FileInputStream ;
import java. io. FileOutputStream ;
import java. io. IOException ;
public class TestCopy02 {
public static void main ( String [ ] args) throws IOException {
long startTime = System . currentTimeMillis ( ) ;
FileInputStream fip = new FileInputStream ( "D:\\Hello.txt" ) ;
FileOutputStream fop = new FileOutputStream ( "E:\\Hello.txt" ) ;
byte [ ] b = new byte [ 1024 * 8 ] ;
int len = - 1 ;
while ( ( len = fip. read ( b) ) != - 1 ) {
fop. write ( b, 0 , len) ;
}
fip. close ( ) ;
fop. close ( ) ;
long endTime = System . currentTimeMillis ( ) ;
System . out. println ( endTime - startTime) ;
}
}
第三版【异常处理】【重点】【掌握】
package com. javastudy ;
import java. io. FileInputStream ;
import java. io. FileNotFoundException ;
import java. io. FileOutputStream ;
import java. io. IOException ;
public class TestCopy {
public static void main ( String [ ] args) {
FileInputStream fis = null ;
FileOutputStream fio = null ;
try {
fis = new FileInputStream ( "E:\\JavaStudy\\Day19\\src\\com\\javastudy\\hello.txt" ) ;
fio = new FileOutputStream ( "E:\\JavaStudy\\hello.txt" ) ;
int len = - 1 ;
byte [ ] b = new byte [ 1024 * 8 ] ;
while ( ( len = fis. read ( b) ) != - 1 ) {
fio. write ( b, 0 , len) ;
}
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
try {
if ( fis != null ) {
fis. close ( ) ;
}
if ( fio != null ) {
fio. close ( ) ;
}
}
catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
System . out. println ( "OVER" ) ;
}
}
五、缓冲流
5.1 概述
减少IO的次数,提高IO性能 在字节流基础上进行包装
5.2 案例代码
package com. javastudy ;
import java. io. BufferedInputStream ;
import java. io. BufferedOutputStream ;
import java. io. FileInputStream ;
import java. io. FileOutputStream ;
import java. io. IOException ;
public class TestBuffCopy {
public static void main ( String [ ] args) throws IOException {
BufferedInputStream bin = new BufferedInputStream ( new FileInputStream ( "E:\\JavaStudy\\Day19\\src\\com\\javastudy\\hello.txt" ) ) ;
BufferedOutputStream bou = new BufferedOutputStream ( new FileOutputStream ( "E:\\JavaStudy\\hello.txt" ) ) ;
byte [ ] b = new byte [ 1024 * 8 ] ;
int len = - 1 ;
while ( ( len = bin. read ( b) ) != - 1 ) {
bou. write ( b, 0 , len) ;
}
bou. flush ( ) ;
bin. close ( ) ;
bou. close ( ) ;
}
}
六、字符流
6.1 字符流概述
字节流能读写任意类型的文件 一个字符可能是一个或者多个字节组成
使用字节流每次读取一个字节可能会造成读取半个文字 需要一种能按照个数读取数据的流–字符流
6.2 字符输入流
抽象类reader
实现类InputStreamReader
FileReader
创建对象
FileReader(String fileName)
在给定从中读取数据的文件名的情况下创建一个新 FileReader。
读取数据
package com. javastudy ;
import java. io. FileReader ;
import java. io. IOException ;
import java. util. Arrays ;
public class TestInputStreamReader {
public static void main ( String [ ] args) throws IOException {
FileReader reader = new FileReader ( "D:\\Hello.txt" ) ;
char [ ] c = new char [ 4 ] ;
int len;
len = reader. read ( c) ;
System . out. println ( len) ;
System . out. println ( Arrays . toString ( c) ) ;
len = reader. read ( c) ;
System . out. println ( len) ;
System . out. println ( Arrays . toString ( c) ) ;
len = reader. read ( c) ;
System . out. println ( len) ;
System . out. println ( Arrays . toString ( c) ) ;
reader. close ( ) ;
}
}
6.3 字符输出流
抽象类Writer
实现类OutputStreamWriter
FileWriter
创建对象和写入数据
package com. javastudy ;
import java. io. FileWriter ;
import java. io. IOException ;
public class TestOutputStreamWriter {
public static void main ( String [ ] args) throws IOException {
FileWriter writer = new FileWriter ( "E://hehe.txt" ) ;
writer. write ( "斯是陋室惟吾德馨。" ) ;
writer. close ( ) ;
}
}
6.4 使用字符流复制小说
1、创建字符流镀锡
2、循环读写数据
3、关闭流
package com. javastudy ;
import java. io. FileReader ;
import java. io. FileWriter ;
import java. io. IOException ;
public class TestCopy {
public static void main ( String [ ] args) throws IOException {
FileReader read = new FileReader ( "E:\\threeCountry.txt" ) ;
FileWriter write = new FileWriter ( "C:\\Users\\Administrator\\Desktop\\threeCountry.txt" ) ;
char [ ] c = new char [ 1024 ] ;
int len = - 1 ;
while ( ( len = read. read ( c) ) != - 1 ) {
write. write ( c, 0 , len) ;
}
write. close ( ) ;
read. close ( ) ;
}
}
七、转换流
7.1 概述
package com. javastudy ;
import java. io. FileInputStream ;
import java. io. FileOutputStream ;
import java. io. IOException ;
import java. io. InputStreamReader ;
import java. io. OutputStreamWriter ;
public class TestErrorCode {
public static void main ( String [ ] args) throws IOException {
InputStreamReader isr = new InputStreamReader ( new FileInputStream ( "D:/hello.txt" ) , "UTF-8" ) ;
OutputStreamWriter osw = new OutputStreamWriter ( new FileOutputStream ( "E:/hello.txt" ) , "GBK" ) ;
char [ ] c = new char [ 1024 ] ;
int len = - 1 ;
while ( ( len = isr. read ( c) ) != - 1 ) {
System . out. println ( len) ;
osw. write ( c, 0 , len) ;
}
osw. close ( ) ;
isr. close ( ) ;
System . out. println ( "OVER" ) ;
}
}
八、对象流
8.1 概述
8.2 案例
收发JDK内置类型
package com. javastudy ;
import java. io. FileOutputStream ;
import java. io. IOException ;
import java. io. ObjectOutputStream ;
public class TestObjectIO01 {
public static void main ( String [ ] args) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream ( new FileOutputStream ( "file" ) ) ;
oos. writeObject ( 1000 ) ;
oos. writeObject ( 3.14 ) ;
oos. writeObject ( "Hello" ) ;
oos. writeObject ( true ) ;
oos. writeObject ( new int [ ] { 11 , 22 , 33 , 44 , 55 } ) ;
oos. close ( ) ;
System . out. println ( "OVER" ) ;
}
}
package com. javastudy ;
import java. io. FileInputStream ;
import java. io. IOException ;
import java. io. ObjectInputStream ;
public class TestObjectIO02 {
public static void main ( String [ ] args) throws ClassNotFoundException , IOException {
ObjectInputStream ois = new ObjectInputStream ( new FileInputStream ( "file" ) ) ;
System . out. println ( ois. readObject ( ) ) ;
System . out. println ( ois. readObject ( ) ) ;
System . out. println ( ois. readObject ( ) ) ;
System . out. println ( ois. readObject ( ) ) ;
System . out. println ( ois. readObject ( ) ) ;
ois. close ( ) ;
}
}
收发自定义类型
package com. javastudy ;
import java. io. Serializable ;
public class Student implements Serializable {
String name;
int age;
public Student ( String name, int age) {
super ( ) ;
this . name = name;
this . age = age;
}
@Override
public String toString ( ) {
return "Student [name=" + name + ", age=" + age + "]" ;
}
}
package com. javastudy ;
import java. io. FileNotFoundException ;
import java. io. FileOutputStream ;
import java. io. IOException ;
import java. io. ObjectOutputStream ;
public class Main01 {
public static void main ( String [ ] args) throws FileNotFoundException , IOException {
ObjectOutputStream oos = new ObjectOutputStream ( new FileOutputStream ( "D:/hello.txt" ) ) ;
oos. writeObject ( new Student ( "张三" , 12 ) ) ;
oos. close ( ) ;
System . out. println ( "OVER" ) ;
}
}
package com. javastudy ;
import java. io. FileInputStream ;
import java. io. IOException ;
import java. io. ObjectInputStream ;
public class Main02 {
public static void main ( String [ ] args) throws ClassNotFoundException , IOException {
ObjectInputStream ois = new ObjectInputStream ( new FileInputStream ( "D:/hello.txt" ) ) ;
Object obj = ois. readObject ( ) ;
System . out. println ( obj) ;
ois. close ( ) ;
}
}