21.01数据输入输出流的概述和使用
A:数据输入输出流的概述
数据输入流:DataInputStream
数据输出流:DataOutputStream
特点:可以写基本数据类型,可以读取基本数据类型
public static void main ( String[ ] args) throws IOException {
DataInputStream in = new DataInputStream ( new FileInputStream ( "a.txt" ) ) ;
int num = in. readInt ( ) ;
System. out. println ( num) ;
boolean b = in. readBoolean ( ) ;
System. out. println ( b) ;
String s = in. readUTF ( ) ;
System. out. println ( s) ;
double v = in. readDouble ( ) ;
System. out. println ( v) ;
}
private static void writeData ( ) throws IOException {
DataOutputStream ds = new DataOutputStream ( new FileOutputStream ( "a.txt" ) ) ;
ds. writeInt ( 500 ) ;
ds. writeBoolean ( true ) ;
ds. writeUTF ( "hahaha" ) ;
ds. writeDouble ( 3.14 ) ;
ds. close ( ) ;
}
21.02_内存操作流的概述和使用
A:内存操作流的概述
a:操作字节数组
ByteArrayOutputStream
ByteArrayInputStream
b:操作字符数组
CharArrayWrite
CharArrayReader
c:操作字符串
StringWriter
StringReader
public static void main ( String[ ] args) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ;
bos. write ( "aaa" . getBytes ( ) ) ;
bos. write ( "bbb" . getBytes ( ) ) ;
bos. write ( "ccc" . getBytes ( ) ) ;
byte [ ] bytes = bos. toByteArray ( ) ;
String s = new String ( bytes) ;
System. out. println ( s) ;
String s2 = bos. toString ( ) ;
System. out. println ( s2) ;
}
public static void main ( String[ ] args) throws IOException {
byte [ ] bytes = "西部开源科技教育有限公司" . getBytes ( ) ;
ByteArrayInputStream in = new ByteArrayInputStream ( bytes) ;
byte [ ] bytes1 = new byte [ 1024 ] ;
int len = in. read ( bytes1) ;
String s = new String ( bytes1, 0 , len) ;
System. out. println ( s) ;
}
public static void main ( String[ ] args) throws IOException {
FileInputStream in1 = new FileInputStream ( "C:\\Users\\57642\\Desktop\\The Weeknd,Daft Punk - Starboy.mp3" ) ;
FileInputStream in2 = new FileInputStream ( "C:\\Users\\57642\\Desktop\\Kanye West,Daft Punk - Stronger.mp3" ) ;
FileOutputStream allOut = new FileOutputStream ( "C:\\Users\\57642\\Desktop\\The Weeknd.mp3" ) ;
ArrayList< FileInputStream> list = new ArrayList < > ( ) ;
list. add ( in1) ;
list. add ( in2) ;
ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ;
byte [ ] bytes = new byte [ 1024 * 8 ] ;
int len = 0 ;
for ( FileInputStream in : list) {
while ( ( len = in. read ( bytes) ) != - 1 ) {
bos. write ( bytes, 0 , len) ;
}
in. close ( ) ;
}
byte [ ] allBytes = bos. toByteArray ( ) ;
ByteArrayInputStream bin = new ByteArrayInputStream ( allBytes) ;
byte [ ] bytes2 = new byte [ 1024 * 8 ] ;
int len2 = 0 ;
while ( ( len2 = bin. read ( bytes2) ) != - 1 ) {
allOut. write ( bytes2, 0 , len2) ;
allOut. flush ( ) ;
}
allOut. close ( ) ;
System. out. println ( "合并完毕" ) ;
}
public static void main ( String[ ] args) throws IOException {
FileInputStream in1 = new FileInputStream ( "C:\\Users\\57642\\Desktop\\The Weeknd,Daft Punk - Starboy.mp3" ) ;
FileInputStream in2 = new FileInputStream ( "C:\\Users\\57642\\Desktop\\Kanye West,Daft Punk - Stronger.mp3" ) ;
FileOutputStream allOut = new FileOutputStream ( "C:\\Users\\57642\\Desktop\\The Weeknd2.mp3" ) ;
ArrayList< FileInputStream> list = new ArrayList < > ( ) ;
list. add ( in1) ;
list. add ( in2) ;
byte [ ] bytes2 = new byte [ 1024 * 8 ] ;
int len = 0 ;
for ( FileInputStream in : list) {
while ( ( len = in. read ( bytes2) ) != - 1 ) {
allOut. write ( bytes2, 0 , len) ;
allOut. flush ( ) ;
}
in. close ( ) ;
}
allOut. close ( ) ;
}
public static void main ( String[ ] args) throws IOException {
CharArrayWriter charArrayWriter = new CharArrayWriter ( ) ;
charArrayWriter. write ( "abc" ) ;
charArrayWriter. write ( "abc" ) ;
charArrayWriter. write ( "abc" ) ;
charArrayWriter. write ( "abc" ) ;
String s = charArrayWriter. toString ( ) ;
char [ ] chars = charArrayWriter. toCharArray ( ) ;
String s1 = String. valueOf ( chars) ;
System. out. println ( s) ;
System. out. println ( s1) ;
}
public static void main ( String[ ] args) throws IOException {
StringWriter stringWriter = new StringWriter ( ) ;
stringWriter. write ( "abc" ) ;
stringWriter. write ( "abc" ) ;
stringWriter. write ( "abc" ) ;
stringWriter. write ( "abc" ) ;
stringWriter. write ( "abc" ) ;
String s = stringWriter. toString ( ) ;
System. out. println ( s) ;
}
21.03_打印流
A:打印流的特点
字节打印流
字符打印流
a:打印流只能操作目的地,不能操作数据源(不能进行读取数据)
b:可以操作任意数据类型的数据 调用print() 方法可以写任意数据类型
c:如果我们启用自动刷新,那么在调用println、printf或format方法中
的一个方法的时候,就会完成自动刷新
/*
通过以下构造创建对象 能够启动自动刷新 然后调用println、printf或
format方法中的一个方法的时候,会完成自动刷新
* public PrintWriter(OutputStream out, boolean autoFlush)
启动自动刷新
* public PrintWriter(Writer out, boolean autoFlush)
启动自动刷新
d:这个流可以直接对文件进行操作(可以直接操作文件的流:就是构造方
法的参数可以传递文件或者文件路径)
public static void main ( String[ ] args) throws IOException {
PrintStream printStream = new PrintStream ( "e.txt" ) ;
printStream. write ( "你好" . getBytes ( ) ) ;
printStream. println ( "abc" ) ;
printStream. println ( 100 ) ;
printStream. println ( 3.14 ) ;
PrintStream out = System. out;
out. println ( 200 ) ;
out. write ( "hahah" . getBytes ( ) ) ;
}
public static void main ( String[ ] args) throws FileNotFoundException {
PrintWriter pw = new PrintWriter ( new FileOutputStream ( "g.txt" ) , true ) ;
pw. write ( "abc" ) ;
pw. write ( "abc" ) ;
pw. write ( "abc" ) ;
pw. write ( "abc" ) ;
pw. println ( "abc" ) ;
pw. println ( "abc" ) ;
pw. println ( "abc" ) ;
pw. println ( "abc" ) ;
pw. println ( "abc" ) ;
}
public static void main ( String[ ] args) throws IOException {
BufferedReader br = new BufferedReader ( new FileReader ( "g.txt" ) ) ;
PrintWriter pw = new PrintWriter ( new FileOutputStream ( "name.txt" ) , true ) ;
while ( true ) {
String line = br. readLine ( ) ;
if ( line == null) {
break ;
}
pw. println ( line) ;
}
br. close ( ) ;
pw. close ( ) ;
}
21.04_标准输入输出流概述和输出语句
A:标准输入输出流
在System这个类中存在两个静态的成员变量
public static final InputStream in:标准输入流,对应的设备是键盘
public static final PrintStream out:标准输出流,对应的设备就是
显示器
System.in的类型是InputStream
System.out的类型是PrintStream是OutputStream的孙子类
FilterOutputStream的子类
public static void main ( String[ ] args) throws IOException {
BufferedReader br = new BufferedReader ( new InputStreamReader ( System. in) ) ;
while ( true ) {
System. out. println ( "请输入内容" ) ;
String s = br. readLine ( ) ;
if ( "886" . equals ( s) ) {
break ;
}
System. out. println ( s) ;
}
}
public static void main ( String[ ] args) throws FileNotFoundException {
Scanner scanner = new Scanner ( new File ( "demo.java" ) ) ;
PrintWriter pw = new PrintWriter ( "demo2.java" ) ;
while ( scanner. hasNextLine ( ) ) {
String s = scanner. nextLine ( ) ;
pw. println ( s) ;
pw. flush ( ) ;
}
scanner. close ( ) ;
pw. close ( ) ;
}
21.05_随机访问流概述
A:随机访问流概述
RandomAccessFile概述 最大特点 能读能写
RandomAccessFile类不属于流,是Object类的子类。但它融合了
InputStream和OutputStream的功能。
支持对随机访问文件的读取和写入
RandomAccessFile的父类是Object,这个流对象可以用来读取数据也可以
用来写数据,可以操作任意数据类型的数据。
我们通过getFilePointer方法获取文件指针,并且通过seek方法设置文件
指针
public static void main ( String[ ] args) throws IOException {
File file = new File ( "The Weeknd,Daft Punk - Starboy.ncm" ) ;
File file1 = new File ( "C:\\Users\\57642\\Desktop\\333.ncm" ) ;
RandomAccessFile in = new RandomAccessFile ( file, "rw" ) ;
RandomAccessFile out = new RandomAccessFile ( file, "rw" ) ;
if ( ! file1. exists ( ) ) {
in. seek ( 0 ) ;
out. seek ( 0 ) ;
}
BufferedReader br = new BufferedReader ( new FileReader ( "config.txt" ) ) ;
String s = br. readLine ( ) ;
in. seek ( Long. parseLong ( s) ) ;
out. seek ( Long. parseLong ( s) ) ;
byte [ ] bytes = new byte [ 3 ] ;
int len = 0 ;
int i = 0 ;
while ( ( len = in. read ( bytes) ) != - 1 ) {
out. write ( bytes, 0 , len) ;
}
in. close ( ) ;
out. close ( ) ;
}
21.06_序列化流和反序列化流
A:序列化流的概述
所谓的序列化:就是把对象通过流的方式存储到文件中,注意:此对象 要
重写Serializable
反序列化:就是把文件中存储的对象以流的方式还原成对象
序列化流: ObjectOutputStream
反序列化流: ObjectInputStream
public static void main ( String[ ] args) throws IOException, ClassNotFoundException {
readData ( ) ;
}
private static void readData ( ) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream ( new FileInputStream ( "student.txt" ) ) ;
Student student = ( Student) ois. readObject ( ) ;
String name = student. getName ( ) ;
int age = student. getAge ( ) ;
System. out. println ( name) ;
System. out. println ( age) ;
}
private static void writeData ( ) throws IOException {
Student student = new Student ( "张三" , 23 ) ;
ObjectOutputStream oos = new ObjectOutputStream ( new FileOutputStream ( "student.txt" ) ) ;
oos. writeObject ( student) ;
oos. close ( ) ;
}
public static void main ( String[ ] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream ( new FileInputStream ( "student2.txt" ) ) ;
Student student = ( Student) ois. readObject ( ) ;
String name = student. getName ( ) ;
int age = student. getAge ( ) ;
System. out. println ( name) ;
System. out. println ( age) ;
}
public static void main ( String[ ] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream ( new FileInputStream ( "student2.txt" ) ) ;
ArrayList< Student> list = ( ArrayList< Student> ) ois. readObject ( ) ;
Student student = list. get ( 2 ) ;
System. out. println ( student. getName ( ) ) ;
System. out. println ( student. getAge ( ) ) ;
}
21.07_Properties的概述和作为Map集合的使用
A:Properties的概述
Properties 表示一个持久的属性集。
Properties 可保存在流中或从流中加载。
属性列表中每个键及其对应值都是一个字符串。
Properties父类是Hashtable
— 属于双列集合,这个集合中的键和值都是字符串 Properties 不能指定
泛型
B:Properties的特殊功能
public Object setProperty(String key,String value)
public String getProperty(String key)
public Set<String> stringPropertyNames()
C:Properties的load()和store()功能
Properties和IO流进行配合使用:
— public void load(Reader reader):读取键值对数据把数据存储到
Properties中
— public void store(Writer writer, String comments) 把Properties
集合中的键值对数据写入到文件中,comments注释
public static void main ( String[ ] args) {
Properties properties = new Properties ( ) ;
properties. setProperty ( "username" , "张三" ) ;
properties. setProperty ( "password" , "123456" ) ;
String username = properties. getProperty ( "username" ) ;
String password = properties. getProperty ( "password" ) ;
System. out. println ( username) ;
System. out. println ( password) ;
String password2 = properties. getProperty ( "passsword555" , "56789" ) ;
System. out. println ( password2) ;
}
public static void main ( String[ ] args) throws IOException {
LinkedHashMap< String, String> lhm = new LinkedHashMap < > ( ) ;
lhm. put ( "username" , "张三" ) ;
lhm. put ( "password" , "123456" ) ;
Set< Map. Entry< String, String> > entries = lhm. entrySet ( ) ;
BufferedWriter bw = new BufferedWriter ( new FileWriter ( "user.properties" ) ) ;
for ( Map. Entry< String, String> en : entries) {
String key = en. getKey ( ) ;
String value = en. getValue ( ) ;
bw. write ( key + "=" + value) ;
bw. newLine ( ) ;
bw. flush ( ) ;
}
bw. close ( ) ;
}
public static void main ( String[ ] args) throws IOException {
LinkedHashMap< String, String> lhm = new LinkedHashMap < > ( ) ;
BufferedReader br = new BufferedReader ( new FileReader ( "user.properties" ) ) ;
while ( true ) {
String s = br. readLine ( ) ;
if ( s == null) {
break ;
}
String[ ] split = s. split ( "=" ) ;
lhm. put ( split[ 0 ] , split[ 1 ] ) ;
}
System. out. println ( lhm) ;
}
public static void main ( String[ ] args) throws IOException {
Properties properties = new Properties ( ) ;
properties. setProperty ( "username" , "张三" ) ;
properties. setProperty ( "password" , "123456" ) ;
properties. store ( new FileOutputStream ( "user2.properties" ) , null) ;
}
public static void main ( String[ ] args) throws IOException {
Properties properties = new Properties ( ) ;
properties. load ( new FileReader ( "user2.properties" ) ) ;
System. out. println ( properties) ;
}
21.08_SequenceInputStream
A:SequenceInputStream
表示其他输入流的逻辑串联。
它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件
末尾,接着从第二个输入流读取,依此类推,直到到达包含的最后一个输
入流的文件末尾为止
B:构造方法
SequenceInputStream(InputStream s1, InputStream s2)
通过记住这两个参数来初始化新创建的 SequenceInputStream(将按顺序
读取这两个参数,先读取s1,后读取s2),以提供从此
SequenceInputStream 读取的字节
SequenceInputStream(Enumeration < ? extends InputStream > e)
通过记住参数来初始化新创建的 SequenceInputStream,该参数必
须生成运行时类型为 InputStream 对象的 Enumeration 型参数
public static void main ( String[ ] args) throws IOException {
FileInputStream in = new FileInputStream ( "user.properties" ) ;
FileInputStream in1 = new FileInputStream ( "user2.properties" ) ;
SequenceInputStream sis = new SequenceInputStream ( in, in1) ;
FileOutputStream fos = new FileOutputStream ( "hehe.properties" ) ;
byte [ ] bytes = new byte [ 1024 ] ;
int len = 0 ;
while ( ( len = sis. read ( bytes) ) != - 1 ) {
fos. write ( bytes, 0 , len) ;
fos. flush ( ) ;
}
sis. close ( ) ;
}
public static void main ( String[ ] args) throws FileNotFoundException {
FileInputStream in = new FileInputStream ( "user.properties" ) ;
FileInputStream in1 = new FileInputStream ( "user2.properties" ) ;
FileInputStream in2 = new FileInputStream ( "user2.properties" ) ;
SequenceInputStream sis = new SequenceInputStream ( in, in1) ;
SequenceInputStream sis1 = new SequenceInputStream ( sis, in2) ;
}
public static void main ( String[ ] args) throws IOException {
FileInputStream in = new FileInputStream ( "user.properties" ) ;
FileInputStream in1 = new FileInputStream ( "user2.properties" ) ;
FileInputStream in2 = new FileInputStream ( "user2.properties" ) ;
Vector< FileInputStream> vector = new Vector < > ( ) ;
vector. add ( in) ;
vector. add ( in1) ;
vector. add ( in2) ;
Enumeration< FileInputStream> elements = vector. elements ( ) ;
SequenceInputStream sis = new SequenceInputStream ( elements) ;
FileOutputStream fos = new FileOutputStream ( "hehe1.properties" ) ;
byte [ ] bytes = new byte [ 1024 ] ;
int len = 0 ;
while ( ( len = sis. read ( bytes) ) != - 1 ) {
fos. write ( bytes, 0 , len) ;
fos. flush ( ) ;
}
sis. close ( ) ;
}