Collection(集合) API
Set:一個無序無重復;List:一個有序可重復。
Iterators迭代器
如:
List list = new ArrayList();
Iterators elements = list.iterator();
while(elements.hasNext()) {
??????System.out.println(elements.next());
}
Vector向量
轉換成枚舉類型
Enumeration enum1 = p.elements();
while(enum1.hasMoreElements()) {
??????name = (String)enum1.nextElement();
??????System.out.println(name);
}
Hashtable 放入“鍵-值”關系對象。
put(參數1,參數2)
Enumeration enum1 = hash.keys();
Object obj;
while(enum1.hasMoreElements()) {
??????obj = enum1.nextElement();
??????System.out.println(obj+":"+hash.get(obj));
}
Java流
輸入/輸出源抽象為“流”,16位字符流。
字節流:InputStream和OutputStream。
字符流:Reader和Writer。
數據類型分節點流(低級流)和處理流(高級流),高級流和低級流聯系起來使用,低級流和輸入/輸出設備關聯。
System.exit(0)是正常退出,System.exit(1)是非正常退出。
可以通過FileInputStream和FileOutputStream來實現文件的拷貝。
如:
package com.sinojava.one;
import java.io.*;
/*
?? 文件拷貝,將一個文件中的內容拷貝到另一個文件中
*/
public class CopyFile
{
???????public boolean copyFile(String src,String des)
???????{?
???????????File srcFile,desFile;
???????????srcFile = new File(src);
???????????desFile = new File(des);
???????????FileInputStream fis = null;
???????????FileOutputStream fos = null;
?
???????????try{
????????????????desFile.createNewFile();
????????????????fis = new FileInputStream(srcFile);
????????????????fos = new FileOutputStream(desFile);
????????????????int bytesRead;
????????????????byte[] buf = new byte[4 * 1024];? // 4K buffer
????????????????while((bytesRead=fis.read(buf))!=-1){
?????????????????????fos.write(buf,0,bytesRead);? //0到bytesRead字節
????????????????}
????????????????fos.flush();
????????????????fos.close();
????????????????fis.close();
??????????? }catch(IOException e){
???????????????????System.out.println(e);
???????????????????return false;
??????????????}
?????????????return true;?
??????????}?
??????????public static void main(String args[])
??????????{
?????????????????CopyFile cf = new CopyFile();
?????????????????String src = args[0];
?????????????????String des = args[1];
?????????????????if(cf.copyFile(src,des))
?????????????????{
????????????????????????System.out.println("拷貝成功!");
?????????????????}
?????????????????else
?????????????????{
????????????????????????System.out.println("拷貝失敗!");
?????????????????}??
?????????????}
??????}
緩沖流:對節點流封裝,起緩沖作用,提高讀寫效率。
流的幾種經典流向:
A-file---(bytes)FileInputStream---(bytes)DataInputStream---(String) .
A-file---FileInputStream---BufferedInputStream---DataInputStream .
A-file---FileReader---BufferedReader .
重定向標準輸入/輸出,使用System.setIn()和System.setOut()來改變設備。
RandomAccessFile 特殊文件流
它的接口是Data Input 和 Data Output。
有兩個構造方法:
RandomAccessFile(File file,String mode)
RandomAccessFile(String file,String mode)
編碼的轉換(中文出現亂碼現象)
如:
package com.sinojava.one;
import java.io.*;
public class AppendFile
{
??? public static void main(String[] args) {
??????????? String toAppend = args[0];?? //從命令行隨意輸入字符
??????????? try{
????????????int i = 0;
????????????String record = new String();
????????????String toCn = null;
????????????//處理中文問題
????????????toCn = new String(toAppend.getBytes("GBK"),"ISO8859_1"); //將其轉換為ISO8859_1格式
??????
????????????RandomAccessFile rf1?
????????????= new RandomAccessFile("c:/toAppend.txt","rw");?? //rw是固定寫法,表示讀寫操作
????????????rf1.seek(rf1.length());? //返回的是文件的長度
????????????rf1.writeBytes(toCn+"\n");?? //給文件以字節的形式寫入字符串
????????????rf1.close();
????
????????????RandomAccessFile rf2 = new RandomAccessFile("c:/toAppend.txt","r");? //設置從toAppend.txt中讀內容
????????????String outCn = null;
????????????while ((record = rf2.readLine()) != null)?
????????????{
????????????????????i ++;
????????????????????//處理中文問題
????????????????????outCn = new String(record.getBytes("ISO8859_1"),"GBK");??????????????????????????????????????????????????????????????????????//把文件中的ISO8859_1編碼格式轉換為GBK格式
????????
????????????????????System.out.println("Line "+i+":"+outCn);? //最后將讀出來的內容打印到命令控制臺上
????????????}
????????????rf2.close();
????????????}catch(Exception e)
????????????{
???????????????????e.printStackTrace();?
????????????}??
??????}
}