Java基础突击第八天0018(File,RandomAccessFile)

Java IO最重要的:5个类一个接口、

File  OutputStream InputStream  Writer Reader 

Serializable

整个io包中唯一与文件本身有关系的就是File类。

构造public File(String pathname)

pathname -> d:\\test.txt

File类常用 常量

public static final String pathSeparator

public static final String separator

构造

public File(String pathName)

普通

public boolean createNewFile() throws IOException //创建一个文件 有throws需要trycatch

public boolean delete()

public boolean exist()

public boolean isDirectory()

public boolean mkdir()  //创建一个目录

public boolean renameTo(File dest)  //为已有的文件重命名

public long length()  //返回文件大小

public String[] list()  //列出指定目录的全部内容,只是列出了名称

public File[] listFiles() //列出指定目录的全部内容,列出了路径

在WIndows反斜杠用\\

Linux反斜杠用//

用File提供的separator就可以保证在不同系统间代码的可移植性。

import java.io.File;
import java.io.IOException;
public class TestJava{
}
class Demo{
		public static void main(String[] args){
				File filedir = new File("C:"+File.separator+"JavaStudy"+File.separator+
						"JavaIO");
				filedir.mkdir();
				File file01 = new File("C:\\JavaStudy\\JavaIO\\test01.txt");
				File file02 = new File("C:"+File.separator+"JavaStudy"+File.separator+
						"JavaIO"+File.separator+"test02.txt");
				try{
						file01.createNewFile(); //create test01.txt by use \\
						file02.createNewFile();  //create test02.txt by use File.separatord
						if(file02.exists()){
								System.out.println("file02 exists!");
								file02.delete();
						}else{
								System.out.println("file02 doesn't exists!");
						}
						file02.createNewFile();

						String strDir[] = filedir.list();
						for(int i = 0;i< strDir.length;i++){
								System.out.print(strDir[i]+" ");
						}
						System.out.println();

						File filesPrint[] = filedir.listFiles();
						for(int i=0;i<filesPrint.length;i++){
								System.out.print(filesPrint[i]+" ");
						}
						System.out.println();

						if(filedir.isDirectory()){
								System.out.println(filedir.getPath()+"  is directory!");
						}else{
								System.out.println(filedir.getPath()+"  is directory!");
						}
				}catch(IOException e){
						e.printStackTrace();
				}
		}
}//Demo

output:

test01.txt test02.txt
C:\JavaStudy\JavaIO\test01.txt C:\JavaStudy\JavaIO\test02.txt

C:\JavaStudy\JavaIOis directory!


File只是针对文件操作,操作文件内容用RandomAccessFile类。

RandomAccessFile可以随机存取的条件是每个数据长度保持一致。

这个类必须设置模式(String mode),r只读,w只写,rw读写

构造:

public RandomAccessFile(File file,String mode) throws FileNotFoundException

public RandomAccessFile(String name,String mode) throws FileNodeFoundException//name是文件路径

public void close() throws IOException

public int read(byte[] b)throws IOException  //将内容读取到一个byte数组中。

public final byte readByte() throws IOException    //读取一个字节,final

public final int readInt() throws IOException  //从文件中读整型数据,final

public final void writeByte(String s) throws IOException//将一个字符串写入到文件,按字节方式处理

public final void writeInt(int v) throws IOException//将一个int数据写入文件,长度为4位

public void seek(long pos) throws IOException//设置读指针的位置

public int skipBytes(int n)throws IOException //指针跳过多少个字节

File fileRdf = new File("C:"+File.separator+"JavaStudy"+File.separator+
						"JavaIO"+File.separator+"rdf.txt");
				//if the file doesn't exist, system will create one
				RandomAccessFile rdf = new RandomAccessFile(fileRdf,"rw");
				String name = null;
				int age = 0;
				name = "FangXiay";
				age = 14;
				rdf.writeBytes(name);
				rdf.writeInt(age);
				name = "FuXiaoSh";
				age = 15;
				rdf.writeBytes(name);
				rdf.writeInt(age);
				name = "Xun_Shuo";
				age = 14;
				rdf.writeBytes(name);
				rdf.writeInt(age);
				rdf.close();

利用RandomAccessFile存入数据如上所示。路径如果不存在,则会自动建立file。每个字符串保证8字节,Int保证4字节,存入TXT文件。

打开txt显示是这个德行的:FangXiay   FuXiaoSh   Xun_Shuo   

估计是编码问题。

import java.io.File;
import java.io.RandomAccessFile;
public class TestJava{
}
class Demo{
		public static void main(String[] args) throws Exception{
				File fileRdf = new File("C:"+File.separator+"JavaStudy"+File.separator+
						"JavaIO"+File.separator+"rdf.txt");
				RandomAccessFile rdf = new RandomAccessFile(fileRdf,"r");
				String name = null;
				int age = 0;
				byte[] bReady = new byte[8];
				rdf.skipBytes(12);
				for(int i=0;i<bReady.length;i++){
						bReady[i] = rdf.readByte();
				}
				name = new String(bReady);
				age = rdf.readInt();
				System.out.println("Second member's info: Name-"+name+",Age-"+age);
				rdf.seek(0);
				bReady = new byte[8];
				for(int i=0;i<bReady.length;i++){
						bReady[i] = rdf.readByte();
				}
				name = new String(bReady);
				age = rdf.readInt();
				System.out.println("First member's info: Name-"+name+",Age-"+age);

				rdf.skipBytes(12); //At first info's rear,skip 8+4 to skip second's info
				bReady = new byte[8];
				for(int i=0;i<bReady.length;i++){
						bReady[i] = rdf.readByte();
				}
				name = new String(bReady);
				age = rdf.readInt();
				System.out.println("Third member's info: Name-"+name+",Age-"+age);
				rdf.close();
		}
}//Demo

output:

Second member's info: Name-FuXiaoSh,Age-15
First member's info: Name-FangXiay,Age-14
Third member's info: Name-Xun_Shuo,Age-14

...



猜你喜欢

转载自blog.csdn.net/u012144068/article/details/80957024