JAVA自學筆記22 1、操作基本數據類型的流 DataInputStream DataOutputStream 數據輸出流允許應用程序以適當方式將基本的Java數據類型寫入輸出流中。然后,應用程序可以使用數據輸入流將數據讀入。
private static void write (){
DataOutputStream dos-
new DataOutputStream(
new FileOutputStream(
"dos.txt" ));dos.writeByte(
10 );
dos.writeShort(
100 );dos.cllose();
}
private static void read (){
DataInputStream dis=
new DataOutputStream(
new FileInputStream(
"dos.txt" )
);
byte b=dis.readByte();
short s=dis.readShort();dis.cose();
System.
out .println(b);
System.
out .println(s);
}
2、內存操作流 -操作字節數組 ByteArrayInputStream 包含一個內部緩沖區,該緩沖區包含從流中讀取的字節。 ByteArrayOutputStream 此類實現一個輸出流,其中的數據被寫入一個byte數組,緩沖區會隨著數據的不斷寫入而自動增長。無法關閉該流。
ByteArrayOutputStream baos=
new ByteArrayOutputStream();
for (
int x=
0 ;x<
10 ;x++){
baos.write((
"hello" +x).getBytes());
}
byte [] bys=baos.toByteArray();ByteArrayInputStream bais=
new ByteArrayInputStream(bys);
int by =
0 ;
while ((
by =bais.read())!=-
1 ){
System.out.println((
char )
by );
}baos.close();
-操作字符數組 CharArrayReader CharArrayWrite -操作字符串 StringReader StringWriter
3、打印流 1)分類: 字節打印流 字符打印流 2)特點: 只能操作目的地,不能操作數據 可以操作任意類型的數據 如果啟動了自動刷新,就能夠自動刷新 可以操作文件的流 只能寫,不能讀 注:能直接操作文本文件的有: FileInputStream FileOuputStream FileReader FileWriter PrintStream PrintWriter 流分為基本流和高級流。基本流是能直接讀寫文件的,高級流是在基本流的基礎上提供了一些其他的功能。查看API,看流對象的構造方法,如果同時有File類型和String類型的參數,一般來說都是可以直接操作文件的。
作為Writer的子類使用PrintWriter pw-new PrintWriter(
"pw.txt" );
pw.
write (
"good" );
pw.
write (
"wood" );pw.
close ();
PrintWriter pw
= new PrintWriter(
new FileWriter(
"pw2.txt" ),
true );
pw
. println(
true );
pw
. println(
100 );
bw . write()
bw . newLine()
bw . flush() 打印流改進復制文本文件
BufferedReader br=
new BufferedReader(
new FileReader(
"DataStreamDemo.java" ));
PrintWriter pw=
new PrintWriter(
new FileWriter(
"copy.java" ,
true ));
String line=
null ;
while ((line=br,readLine())!=
null ){
pw.println(line);
}
pw.close();
br.close();
3)標準輸入輸出流 System類中的字段:in,out public static final InputStream in “標準”輸入流 public static final PrintStream out “標準”輸出流 它們各代表了系統標準的輸入和輸出設備。默認輸入設備是鍵盤,輸出設備是顯示器。System.in的類型是InputStream.System.out的類型是PrintStream
System.
out .println(
"Hello" );
PrintStream ps=System.
out ;
ps.println(
"helloworld" );
//三種方式實現鍵盤錄入
//獲取標準輸入流
InputStream
is =System.
in ;
//一次獲取一行數據
InputStreamReader isr=
new InputStreamReader(
is );
BufferedReader br=
new BufferedReader(isr);
//或
//BufferedReader br=
new BufferedReader(
new InputStreamReader(System.
in ));
String line=br.readLine();
System.
out .printlb(
"please input words" );
String line=br.readLine();
System.
out .println(line);
BufferedWriter
bw = new BufferedWriter(
new OutputStreamWriter(System
. out);
bw . write(
"hello" );
bw . flush();
bw . close();
6、 1)序列化流 ObjectOutputStream 將Java對象的基本數據類型和圖形寫入OutputStream.把對象按照流一樣的方式存入文本文件或者在網絡中傳輸 2)反序列化流 ObjectInputStream 把文本文件中的流對象或者網路中的流對象數據還原成對象
private ststic
void write (){
ObjectOutputStream cos=
new ObjectOutputStream(
new FileOutputStream(
"cos.txt" ));Person p=
new Person(
"cc" );oos.writeObject(p);oos.close();
}
private static void read ()
{
ObjectInputStream ois=
new ObjextInputStream(
new FileInputStream(
"oos.txt" ));
Object obj=ois.readObject();ois.close();
}
3)如何讓對象不被序列化? 同一個類中有很多成員,有些不想被序列化。這時可以使用transient關鍵字聲明不需要序列化的成員變量。可以與IO流結合來使用。Hashtable的子類,說明是一個Map集合
7、Properties集合 1)表示一個持久的屬性集。Prooerties可保存在流中或從流中加載,屬性列表中每個鍵及其對應值都是一個字符串。 2)構造方法 無參:沒有泛型 Properties prop=new Properties(); prop.put(key,value);
prop.put(
"001" ,
"cc" );
prop.put(
"002" ,
"dc" );
prop.put(
"003" ,
"ac" );
Set<
Object >
set =.prop.keySet();
for (
Object key:
set ){
Object value=prop.
get (key);
}
3)特殊功能 public Object Property(String key,String value) 添加元素 public Object getProperty(String key) 獲取元素 public Set stringPropertyNames() 獲取所有的鍵的集合
Properties prop=new Propertiess()prop
.setProperty (
"ww" ,
"12" )
prop
.setProperty (
"we" ,
"12" )
prop
.setProperty (
"wq" ,
"13" )
Set <String>
set =prop
.stringPropertyNames ()
for(String key:
set ){
String value=prop
.getProperty (key)
}
必須是Properties集合,文件的形式必須是鍵值對的形式 public void load(Reader reader) 把文件的數據讀取到集合中 public void store(Writer writer,String commments) 把集合中的數據存儲到文件
Properties prop=new Properties()//將文件中的數據讀入到集合中
Reader r=new FileReader(
"prop.txt" )
prop
.load (r)
r
.close ()
System
.out .println (
"prop:" +prop)//寫數據
Properties prop=new Properties()
prop
.setProperty (
"ww" ,
"3" )
prop
.setProperty (
"w3" ,
"5" )
prop
.setProperty (
"w2" ,
"6" )Writer w=new FileWriter(
"name.txt" )
prop
.store (w,
"men" )
@例題1:判斷文件中是否有指定的鍵,如果有就把其值改為100 -把文件中的數據加載到集合中 -遍歷集合,獲得每一個鍵 -判斷是否有Lisi,如果有就修改值為100 -把集合中的數據重新存儲到文件中
Properties prop
= new Properties();
Reader r
= new FileReader(
"user.txt" );
prop
. load(r);
r
. close();
Set < String > set = prop
. stringPropertyNames();
for(
String key:
set ){
if (
"lisi" . equals (key)){
if (
"lisi" . equals (key)){
prop
. setProperty(key,
"100" );
break;
}
}
Writer w
= new FileWriter(
"user.txt" );
prop
. store(w,
null );
w
. close();
}
Properties prop=
new Properties();
Reader=
new FileReader(
"count.txt" );
prop.load(r);
r.close();String
value =prop.getProperty(
"count" );
int number=Integer.parseInt(
value );
if (number>
5 )
{
System.
out .println(
"請付費" );
System.exit(
0 );
}
else {
number++;
prop.setProperty(
"count" ,String.valurOf(number));
Wwrite w=
new FileWriter(
"count.txt" );
prop.store(w,
null );
w.close();
GuessNumber.start();
}
4、NIO包下的IO流 1)NIO即新IO的意思
Path:路徑 Paths:有一個靜態方法返回一個路徑 public static Path get(URI uri) Files:提供了靜態方法供使用 public static long copy(Path source,OutputStream out)
Files.copy(Paths.
get (
"ByteArrayStreamDemo.java" ),
new FileOutputStream(
"Copy.java" ));
轉載于:https://www.cnblogs.com/Tanqurey/p/10485331.html
總結
以上是生活随笔 為你收集整理的JAVA自学笔记22 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。