文件操作相关
列出某个目录中的所有文件
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 |
package
com.ylfeiu.io; import java.io.File; public class ShowFileInfo { public
static void main(String[] args) { File f = new
File( "d:"
+ File.separator + "txt" ); show(f); } private
static void show(File f) { if
(f.isDirectory()) { File[] res = f.listFiles(); if
(res != null ) { for
( int i = 0 ; i < res.length; i++) { show(res[i]); } } } System.err.println(f); } } |
创建带目录的文件
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 |
package
com.ylfeiu.io; import java.io.File; import java.io.IOException; public class CreateDirectorFile { public
static void main(String[] args) { File f = new
File( "d:"
+ File.separator + "test"
+ File.separator + "hello.txt" ); if
(!f.getParentFile().exists()) { f.getParentFile().mkdirs(); } else
{ try
{ f.createNewFile(); } catch
(IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } |
字节流与字符流
输入输出流,分为字节流(InputStream,OutPutStream),字符流(Writer,Reader),它们4个都是抽象类,实例化靠其子类对象;传输中用字节流.字符流在操作时用到了缓冲区,若在使用字符流操作后,不关闭输出流操作,那么该缓冲区的内容不会被输出,这个时候得去调用flush()强制清空缓冲区的内容进行输出.字节流这不存在该情况
java.lang.Object
|-java.io.InputStream
|-java.io.FileInputStream
java.lang.Object
|-java.io.Reader
|-java.io.InputStreamReader
|-java.io.FileReader
通过以上继承关系可以看出,所有字符数据都是需要转换的,依靠转换流操作,真正保存或传输的数据是以字节流来完成的
字节流与字符流的转换采用:OutputStreamWriter,InputStreamReader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 |
package
com.ylfeiu.io; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; public class CreateDirectorFile { public
static void main(String[] args) throws
Exception { File f = new
File( "d:"
+ File.separator + "test"
+ File.separator + "hello.txt" ); if
(!f.getParentFile().exists()) { f.getParentFile().mkdirs(); } else
{ f.createNewFile(); } OutputStream out = new
FileOutputStream(f); Writer w = new
OutputStreamWriter(out); w.write( "你好啊" ); w.flush(); } } |
构造:输入流构造需要传参(操作对象),输出流构造函数中没有操作对象
public ByteArrayInputStream(byte buf[])
使用ByteArrayInputStream,ByteArrayOutputStream对内存进行输入输出操作
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 |
package
com.ylfeiu.io; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; public class ByteArrayIO { public
static void main(String[] args) throws
Exception { String str = "hello" ; InputStream in = new
ByteArrayInputStream(str.getBytes()); OutputStream w = new
ByteArrayOutputStream(); int
tmp = 0 ; while
((tmp = in.read()) != - 1 ) { w.write(( char )Character.toUpperCase(tmp)); } w.close(); in.close(); String newStr = w.toString(); System.err.println(newStr); } } |
PrintStream,PrintWriter
构造public PrintStream (OutputStream out)
这里虽然传参的是OutputStream型的对象,但是打印流使用的是打印流的方法,并不是使用OutputStream的子类的实际对象的方法
System类的err,out,in这三个常量都是PrintStream的实例(该3个常量在JDK早期出现,所以并不是大写的)
System.out本身就属于PrintStream的对象,而它又是OutputStream的子类,所以有了下面的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13 |
package
com.ylfeiu.io; import java.io.OutputStream; public class SysIn { public
static void main(String[] args) throws
Exception { OutputStream out = System.out; String info = "你好啊 world" ; out.write(info.getBytes()); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 |
打印流例子 package
com.ylfeiu.io; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; public
class PrintIO { public
static void main(String[] args) throws
Exception { String str = "hello world!" ; InputStream in = new
ByteArrayInputStream(str.getBytes()); OutputStream out = new
ByteArrayOutputStream(); PrintStream echo = new
PrintStream(out); int
tmp = 0 ; while
((tmp = in.read()) != - 1 ) { echo.print(( char )Character.toUpperCase(tmp)); } String newStr = out.toString(); out.close(); in.close(); System.err.println(newStr); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 |
中文乱码的产生 package
com.ylfeiu.io; import java.io.InputStream; /** * * @author Administrator 本程序演示中文乱码的产生 */ public
class MessyCode { public
static void main(String[] args) throws
Exception { InputStream in = System.in; StringBuffer buf = new
StringBuffer(); System.out.println( "请输入数据" ); int
tmp = 0 ; while
((tmp = in.read()) != - 1 ) { if
(tmp == ‘\n‘ ) { break ; } buf.append(( char ) tmp); } System.err.println( "输入的内容是:"
+ buf); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 |
BufferedReader缓冲区操作 从缓冲区读取数据 解决中文输入产生的乱码 package
com.ylfeiu.io; import java.io.BufferedReader; import java.io.InputStreamReader; public
class BufRead { public
static void main(String[] args) throws
Exception { BufferedReader buf = new
BufferedReader( new
InputStreamReader(System.in)); System.out.println( "请输入数据" ); String info = buf.readLine(); System.err.println( "输入地 数据是:"
+ info); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 |
Scanner(放在java.utils包中) Jdk1. 5 之后推出的,方便进行输入操作,在程序开发过程中,如果通过程序输出,则采用PrintStream打印流,若存在中文,推荐使用PrintWriter,而对于程序输入数据,建议使用Scanner 例子 package
com.ylfeiu.io; import java.util.Scanner; public class ScannerInput { public
static void main(String[] args) { Scanner in = new
Scanner(System.in); System.err.println( "请输入数据" ); if
(in.hasNext()) { String info = in.next(); System.err.println( "输入的数据是:"
+ info); } } } |
对象输入/出流
采用ObjectInputStream,ObjectOutputStream进行对象的输入与输出,这两个类与打印流的设计相似,都是需要接收父类的实例化对象
例子
对象序列化
对象序列化可以将内存中的对象转换成2进制数据(必须实现Serializable接口),如果不想某个对象的属性被序列化,则采用transient关键字
例子
把对象信息写入到文件中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 |
package
com.ylfeiu.io; import java.io.File; import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjInputIO { public
static void main(String[] args) throws
Exception { Person p = new
Person( 20 , "ylfeiu" ); File f = new
File( "d:"
+ File.separator + "p.ser" ); ObjectOutputStream out = new
ObjectOutputStream( new
FileOutputStream(f)); out.writeObject(p); out.close(); } } class
Person implements
Serializable { transient
int
age; String name; Person( int
age, String name) { this .age = age; this .name = name; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 |
从二进制文件中读取数据 package
com.ylfeiu.io; import java.io.File; import java.io.FileInputStream; import java.io.ObjectInputStream; public class ObjReadIO { public
static void main(String[] args) throws
Exception { File f = new
File( "d:"
+ File.separator + "p.ser" ); ObjectInputStream in = new
ObjectInputStream( new
FileInputStream(f)); Person p = (Person) in.readObject(); in.close(); System.err.println(p.name); // 若给age字段添加private属性,则编译不通过 System.err.println(p.age); } } |
GBK/GB2312:表示国际中文编码,其中GBK包含简体与繁体中文,而GB2312只包含简体中文ISO 8859-1:一种国际通用编码,可以表示任何文字?查一下
Unicode:一种16进制编码,可以便是任何语言文字?
UTF-8:部分编码使用了Unicode,而另一些则采用了ISO-8859-1,它适合网络传输,在项目开发过程中推荐采用此编码?(为什么)
产生乱码的原因:编码与解码不统一
原文:http://www.cnblogs.com/ylfeiu/p/3597315.html