java 6 基础_Java基础6

unit3 流

1.File类:java.io.File包,Serializable和Comparable接口。与系统无关的类。

file:文件  directory:文件夹、目录  path:路径

静态成员变量:

50c9c67cdf017f1aee1a5ff8659f021a.png

绝对路径,相对路径:

65cfbe3502bb741ec342c21ac58f1796.png

构造方法:

File parent1 = new File("C:\\");

File file = new File("hello.java");

File f = new File(parent,child);

File f = new File("F:\\test\\only.txt");

获取方法:

5a6303676df468a712a18a4149996b8f.png

判断功能:

263d6b52ff3e26a2ea82316665cd1eb6.png

创建删除:

e343ff6f9091f535eaf96520591b3f4a.png

599a3d670e0fdf50b6b71e9c6300d9fa.png

File c = new File("F:\\test\\sjw.txt");

try {

boolean d = c.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

System.out.println(c);

删除直接在硬盘删除,不走回收站,所以慎重。可以删除文件和文件夹。

文件遍历:

6c607074184a810c8eec399267e6bf76.png

File c = new File("F:\\test");

String[] arr = c.list();

for (String fileName : arr) {

System.out.println(fileName);

File c = new File("F:\\test");

File[] files = c.listFiles();

for (File file : files) {

System.out.println(file);

递归概念:

4058f435ee42a638ffdeb4d559cb1337.png

//Exception in thread "main" java.lang.StackOverflowError

例子,求和:

public static int sum(int n) {

if(n == 1){

return 1;

}

return n + sum(n-1);

}

例子,递归打印多级目录:

public static void getAllFile(File dir){

File[] files = dir.listFiles();

for (File file : files) {

System.out.println(file);

}

}

例子,文件搜索:文件过滤器:

private static void getAllFile(File dir) {

File[] files = dir.listFiles();

for(File f:files){

if(f.isDirectory()){

getAllFile(f);

}else{

f.getName();

System.out.println(f);

}

}

}

文件过滤器具体运用:FileFilterImpl过滤器实现类,,,,FileNameFilter匿名内部类

cfe0786cacef7027172020a7fd9fb7d5.png

2.IO概述:java.io包下

d3e1877759101930ea6b0ee789ef762d.png

字节流:字节输出流,字节输入流

A:字节输出流:把内存数据写入硬盘文件中去

6adf266dad76ce5d414a18c7985291d8.png

47f689b73208f671f7b7101a19c600b1.png

33984b81f0e571a4c524e5ac9ea3dd18.png

public static void main(String[] args) throws IOException {

//1.创建对象,构造方法传递写入数据目的地

FileOutputStream f = new FileOutputStream("F:\\test\\only.txt");

//2.调用write方法

f.write(Integer.parseInt("22"));

//3.释放资源

f.close();

}

B:字节输入流:读取出内存数据

e7b15883f39464502dafc67942cb5fea.png

825797d3bd21ce0f598832cff7cc7bc9.png

//1.创建对象,绑定数据源

FileInputStream f = new FileInputStream("F:\\\\test\\\\only.txt");

//2.read方法读取

int len = f.read();

System.out.println(len);

//循环遍历读取

int len1 = 0;

while( (len1 = f.read()) != -1){

System.out.println((char)len1);

}

//3.释放资源

f.close();

复制粘贴文件:

//创建对象

FileInputStream f = new FileInputStream("F:\\test\\only.txt");

FileOutputStream f1 = new FileOutputStream("F:\\test\\onlycopy.txt");

//读取

int len = 0;

while ((len = f.read()) != -1 ) {

//打印输出

f1.write(len);

}

//资源释放:先关闭写的,后关闭读的

f.close();

f1.close();

字符流:字节流读取中文产生乱码,所以改用字符流读写中文

A,字符输入流:java.io.Reader

29dec9e3a12f14a063445726ecd00c19.png

a7aac184af05dceb568e3b77c9b01cdd.png

public static void main(String[] args) throws IOException {

FileReader f = new FileReader("F:\\test\\onlycopy.txt");

int len = 0;

while ((len = f.read()) != -1) {

System.out.println((char)len);

}

f.close();

}

B,字符输出流:java.io.Writer

100fb37d9f5db18ee2792c6bdadd9797.png

e5496fcfa407a91d42b38d6417a41eff.png

public static void main(String[] args)throws IOException {

FileWriter f = new FileWriter("F:\\test\\new.txt");

f.write(97);

//f.flush();

f.close();

//flush和close,都可以刷新写入字符

}

d8dc6318f68bfb0728e8db6ae71cf4b3.png

使用try--catch,finally处理异常,不用 throw exception

public static void main(String[] args) {

FileWriter f = null;

try {

//可能产生异常的代码

f = new FileWriter

("F:\\test\\only.txt", true);

for (int i = 0; i < 10; i++) {

f.write("hello,sjw" + i + "\r\n");

}

} catch (IOException e) {

//异常处理逻辑

e.printStackTrace();

} finally {

//一定执行的代码

try {

//申明抛出异常对象

f.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

3.字节输出流

4.字节输入流

5.缓冲流:高效读写

68c0f4216bed99ef8095ce9a5f69d6e4.png

都是继承关系,也继承成员方法和构造方法

BufferedWriter:字符缓冲输出流

4dee9ca1cbfe50729351169a731e5feb.png

public static void main(String[] args) throws IOException {

BufferedWriter b = new BufferedWriter(new FileWriter("F:\\test\\1.doc"));

for (int i = 0; i < 3; i++) {

b.write("writer");

b.newLine();//===b.write("\r\n");

}

b.flush();

b.close();

}

BufferedReader:字符缓冲输入流

特有方法:String   readLine()    读一行文本

BufferedOutputStream:

public static void main(String[] args) throws IOException {

//创建文件输出流对象,确定写入的文件地址

FileOutputStream f = new FileOutputStream("F:\\test\\shuang.txt");

//创建数据缓冲输出流

BufferedOutputStream b = new BufferedOutputStream(f);

b.write("数据写入到内部缓冲区中".getBytes());//写入内容

b.flush();//刷新

b.close();//关闭

}

BufferedInputStream:

public static void main(String[] args) throws IOException {

FileInputStream f = new FileInputStream("F:\\test\\shuang.txt");

BufferedInputStream b = new BufferedInputStream(f);

// int len = 0;

// while((len = b.read()) != -1){

// System.out.println(len);

// } //逐个读取

byte[] bytes = new byte[1024];

int len = 0;

while((len = b.read(bytes)) != -1){

System.out.println(len);

} //一起读取

b.close();

}

缓冲流效率测试:

public static void main(String[] args) throws IOException {

long s = System.currentTimeMillis();

//创建字节缓冲输入流对象

BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\test\\1.jpg"));

//创建字节缓冲输出流对象

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\11.jpg"));

int len = 0;

//读出

while ((len = bis.read())!=-1){

//写入

bos.write(len);

}

//释放资源

bos.close();

bis.close();

long e = System.currentTimeMillis();

System.out.println("复制文件耗时:" + (e-s) + "ms");

}

6.转换流:转换编码  ,可以指定编码表进行转换

82337d4f0c258fa8affb5e96f4df6552.png

058cd6fc733e8536e22eba72dbe16b51.png

编码的问题:只能读取UTF-8(默认格式)

A:文本txt----》字节输入流FileInpStream----》FileReader----》字节转换为字符:解码                  乱码

B:文本txt----》字节输入流FileInpStream----》InpStreamReader----》字节转换为字符               不是乱码

8aa380f30647b2b3f0024265eba7284d.png

44c5bb33531e73e8de2a930e165f71c7.png

private static void write_utf_8() throws IOException {

OutputStreamWriter o = new OutputStreamWriter

(new FileOutputStream("F:\\test\\utf_8.txt"), "utf-8");

o.write("你好");

o.flush();

o.close();

}

3570f318c5f9c26784444d03f9c63647.png

bf9838ec466e888f06308ac941867c3b.png

private static void read_utf_8 () throws IOException {

InputStreamReader o = new InputStreamReader

(new FileInputStream("F:\\test\\utf_8.txt"), "utf-8");

int len = 0;

while((len = o.read()) != -1){

System.out.println(len);

}

o.close();

}

7.序列化流:对象以流的方式写入到文件中存储,也叫做对象的序列化

ObjectOutputStream:writeObject()    将对象写入文件中

ObjectInputStream:readObject()       读取对象,是对象的反序列化

c77ac973a2f66c02ef081680085ab1fc.png

7c9b908fd448a93329dbf3d3faff0fd3.png

public class Person implements Serializable

序列化:

public static void main(String[] args) throws IOException {

ObjectOutputStream o = new ObjectOutputStream

(new FileOutputStream("F:\\test\\person.txt"));

o.writeObject(new Person("sjw",12));

o.close();

}

反序列化:

public static void main(String[] args) throws IOException, ClassNotFoundException {

ObjectInputStream o = new ObjectInputStream

((new FileInputStream("F:\\test\\person.txt")));

Object obj = o.readObject();

o.close();

System.out.println(obj);

//Person{name='sjw', age=12}

//结合ObjectOutputStream

}

瞬态关键字:不能被序列化

6a62f948666e23bb72f9f5951ef3e5f7.png

8.打印流:用来打印,java.io.PrintStream,只负责输出

b10bff8db48808db188de1e0d9e88864.png

f685ecf62e2dc57bc79231f7209416ff.png

PrintStream p = new PrintStream("F:\\test\\only.txt");

p.println("22");

p.close();

9.补充:Pproperties集合

64a8db3a67cee15cb1b9de525d4f01b5.png

31119cd781a1bb86bb3b905def9a1a9d.png

8e3980f77cac76d7c35365cce66ff1af.png

18b2f8988745fb7b49f1a4bbf21154a0.png

1ebb5c4668661f220603d031a17f27b3.png

Properties p = new Properties();

p.setProperty("sjw","11");

p.setProperty("linzi","15");

//取出键,存到set集合

Set s = p.stringPropertyNames();

for (String key : s) {

String value = p.getProperty(key);

System.out.println(key + "=" + value);

}

=================================================

Properties p = new Properties();

p.setProperty("sjw","11");

p.setProperty("linzi","15");

FileWriter f = new FileWriter("F:\\test\\linzi.txt");

try {

p.store(f,"save data");

} catch (IOException e) {

e.printStackTrace();

}

f.close();

==================================================

Properties p = new Properties();

p.load(new FileReader("F:\\test\\linzi.txt"));

Set s = p.stringPropertyNames();

for (String key : s) {

String value = p.getProperty(key);

System.out.println(key + "=" + value);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值