Java IO浅析

时间:2021-07-23 06:37:54

1.File类

/**
*
* @author lenovo
*
* File类
* pathSeparator
* separator
*
* File()
* boolean createNewFile()
* boolean delete()
* boolean exists()
* boolean isDirectory()
* length
* list()
*/
public class FileDemo {
public static void main(String[] args) {
/**
* 1.create new file
*/
//
// File f=new File("F:\\test.txt");
// try {
// f.createNewFile();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } /**
* 2.delete exist file
*/ // File f=new File("F:\\test.txt");
// if(f.exists()){
// f.delete();
// } /**
* 3.make directory
*/
// File f=new File("F:"+File.separator+"FileTest");
// f.mkdir(); /**
* 4.list ()
*/ // File f=new File("F:"+File.separator+"FileTest");
// String str[]=f.list();
// for(int i=0;i<str.length;i++){
// System.out.println(str[i]);
// } /**
* 5.listFiles()
*/ // File f=new File("F:"+File.separator+"FileTest");
// File files[]=f.listFiles();
// for(int i=0;i<files.length;i++){
// System.out.println(files[i]);
// } /**
* 6.isDirectory()
*/ // File f=new File("F:"+File.separator+"FileTest"+File.separator+"1.txt");
// if(f.isDirectory()){
// System.out.println("is Directory");
// }else{
// System.out.println("not directory");
// }
}
}

  2.RandomAccessFile类

public class RandomAccessFileDemo {
public static void main(String[] args) throws Exception{
/**
* 写入
*/ // File f=new File("F:"+File.separator+"FileTest"+File.separator+"1.txt");
// RandomAccessFile rdf=null;
// if(f.exists()){
// try {
// rdf=new RandomAccessFile(f, "rw");
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// String name=null;
// int age=0;
// name="zhangsan";
// age=20;
// rdf.writeBytes(name);
// rdf.writeInt(age);
//
// name="lisi ";
// age=30;
// rdf.writeBytes(name);
// rdf.writeInt(age);
//
// name="wangwu ";
// age=10;
// rdf.writeBytes(name);
// rdf.writeInt(age);
//
// rdf.close();
// }else{
// System.out.println("文件不存在!");
// } /**
* 读取
*/
File f=new File("F:"+File.separator+"FileTest"+File.separator+"1.txt");
RandomAccessFile rdf=null;
rdf=new RandomAccessFile(f, "r");
String name=null;
int age=0;
byte b[]=new byte[8];
rdf.skipBytes(12);
for(int i=0;i<b.length;i++){
b[i]=rdf.readByte();
}
name=new String(b);
age=rdf.readInt();
System.out.println(name+age); rdf.seek(0);
for(int i=0;i<b.length;i++){
b[i]=rdf.readByte();
}
name=new String(b);
age=rdf.readInt();
System.out.println(name+age); rdf.skipBytes(12);
for(int i=0;i<b.length;i++){
b[i]=rdf.readByte();
}
name=new String(b);
age=rdf.readInt();
System.out.println(name+age);
}
}

  3.JavaIO的操作流程

  1)使用File类打开一个文件

  2)使用字节流或字符流的子类指定输出的位置

  3)进行读写操作

  4)关闭输入/输出

  4.FileOutputStream

public class FileOutPutStreamDemo {
public static void main(String[] args) throws Exception{ String str="hello world!";
byte b[]=str.getBytes();
/**
* 写(覆盖)
*/
// File f=new File("F:"+File.separator+"FileTest"+File.separator+"2.txt");
// OutputStream out=new FileOutputStream(f);
//// out.write(b);//1.一次性写入
//// out.close();
//
// for(int i=0;i<b.length;i++){
// out.write(b[i]);//2.依次写入
// }
// out.close(); /**
* 写(追加)
*/
// File f=new File("F:"+File.separator+"FileTest"+File.separator+"2.txt");
// OutputStream out=new FileOutputStream(f,true);
// out.write(b);
// out.close(); /**
* 写(换行追加)
*/
// String str1="\r\n hello world!";
// byte b1[]=str1.getBytes();
// File f=new File("F:"+File.separator+"FileTest"+File.separator+"2.txt");
// OutputStream out=new FileOutputStream(f,true);
// out.write(b1);
// out.close();
}
}

  5.FileInputStream

public class FileInputStreamDemo {
public static void main(String[] args) throws Exception{
File f=new File("F:"+File.separator+"FileTest"+File.separator+"2.txt");
InputStream input=new FileInputStream(f);
int len=0;
byte b[]=new byte[1024];
int temp=0; while((temp=input.read())!=-1){
b[len]=(byte)temp;
len++;
}
input.close();
System.out.println(new String(b,0,len));
}
}

  6.FileWriter

	public static void main(String[] args) throws Exception{
String str="\r\n hello world!";
File f=new File("F:"+File.separator+"FileTest"+File.separator+"2.txt");
Writer out=new FileWriter(f,true);
out.write(str);
out.close();
}

  7.FileReader

public static void main(String[] args) throws Exception{
File f=new File("F:"+File.separator+"FileTest"+File.separator+"2.txt");
Reader reader=new FileReader(f);
int len=0;
char c[]=new char[1024];
int temp=0; // while((temp=input.read())!=-1){
// b[len]=(byte)temp;
// len++;
// }
len=reader.read(c); reader.close();
System.out.println(new String(c,0,len));
}