總結出處:http://www.cnblogs.com/lich/archive/2011/12/10/2283445.html
File類的操作
//創建一個文件下的某個文件,先創建文件夾,否則會創建失敗
//File 方法調用1.判斷目錄是否存在,2.創建目錄,3.判斷文件是否存在,4.刪除文件 5.創建文件
//6列出目錄下所有文件名稱 6.得到目錄下所有文件url
File fdir = new File("C:\\Users\\Administrator\\Desktop"+File.separator+"basic");
if(fdir.isDirectory()){
System.out.println("is directory");
}else{
System.out.println("is not directory,mkdir");
fdir.mkdir();
}
File ftxt1 = new File("C:\\Users\\Administrator\\Desktop"+File.separator+"basic"+File.separator+"ftxt1.txt");
if(ftxt1.exists()){
System.out.println("ftxt1 is exists");
//ftxt1.delete();
}else{
try {
System.out.println("ftxt1 is not exists,creat");
ftxt1.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String[] fStrs=fdir.list();
for(int i=0;i<fStrs.length;i++){
System.out.println(fStrs[i]);
}
File[] files = fdir.listFiles();
for(File f:files){
System.out.println(f);
}
RandomAccessFile類
之前的File類只是針對文件本身進行操作的,而如果相對文件內容進行操作,則可以使用RandomAccessFile類,此類屬於隨即讀取類,可以隨機的讀取一個文件中指定位置的數據。因爲在文件中,所有得內容都是按照字節存放的,都有固定的保存位置。
public static void randomAccessFormText() throws IOException{
File file = new File("C:\\Users\\Administrator\\Desktop"+File.separator+"basic"+File.separator+"ftxt2.txt");
RandomAccessFile rf = new RandomAccessFile(file, "rw");
String name1 = "肖";
int age1 = 10;
String name2="tto";
int age2 = 26;
rf.writeUTF(name1);
rf.writeInt(age1);
rf.writeBytes(name2);
rf.writeInt(age2);
rf.close();
RandomAccessFile raf = new RandomAccessFile(file, "r");
raf.skipBytes(9);
byte[] bs = new byte[3];
for(int i=0;i<bs.length;i++){
bs[i] = raf.readByte();
}
String n2 = new String(bs);
int ag2 = raf.readInt();
System.out.println(n2+"\t"+ag2);
raf.seek(0);
System.out.println(raf.readUTF()+"\t"+raf.readInt());
}
字節流字符流
public static void fileInputOutput() throws IOException{
//寫入 字節流
File file = new File("C:\\Users\\Administrator\\Desktop"+File.separator+"basic"+File.separator+"ftxt2.txt");
OutputStream os = new FileOutputStream(file,true);
String s = "Hello word";
os.write(s.getBytes());
os.close();
//讀取 字節流
InputStream is = new FileInputStream(file);
byte[] bt = new byte[(int)file.length()];
is.read(bt);
//System.out.println(new String(bt));
is.close();
//寫入 字符流
Writer writer = new FileWriter(file,true);
String s1 = "Jone";
writer.write(s1);
//不關閉拼不上
writer.close();
//讀出字符流
Reader reader = new FileReader(file);
char[] c = new char[1024];
int len = 0;
int temp = 0;
while((temp=reader.read())!=-1){
c[len] = (char)temp;
len++;
}
System.out.println(new String(c));
}
把一個文件裏的內容拷貝到另一個文件
public static boolean copyFile(File srcFile, File destFile) {
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null;
FileChannel out = null;
try {
fi = new FileInputStream(srcFile);
fo = new FileOutputStream(destFile);
in = fi.getChannel();//得到對應的文件通道
out = fo.getChannel();//得到對應的文件通道
in.transferTo(0, in.size(), out);//連接兩個通道,並且從in通道讀取,然後寫入out通道
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
fi.close();
in.close();
fo.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ByteArrayInputStream 和ByteArrayOutputStream
之前所講解的程序中,輸出和輸入都是從文件中來得,當然,也可以將輸出的位置設置在內存之上,此時就要使用ByteArrayInputStream、ByteArrayOutputStream來完成輸入輸出功能了
ByteArrayInputStream的主要功能將內容輸入到內存之中
ByteArrayOutputStream的主要功能是將內存中的數據輸出
優點:緩存 ,得到數據後可以不僅用來輸出到硬盤,也可以到 顯卡 。。。
此時應該把內存作爲操作點
ByteArrayInputStream和ByteArrayOutputStream
String str="HELLO WORlD!!!";
InputStream input=new ByteArrayInputStream(str.getBytes());
OutputStream output=new ByteArrayOutputStream();
int temp=0;
while((temp=input.read())!=-1){
output.write(Character.toLowerCase(temp));
}
input.close();
output.close();
System.out.println(output.toString());