当前位置: 代码迷 >> Access >> j2se-File种和RandomAccessFile类[转]
  详细解决方案

j2se-File种和RandomAccessFile类[转]

热度:6849   发布时间:2013-02-26 00:00:00.0
j2se----File类和RandomAccessFile类[转]
】http://z466459262.iteye.com/blog/755238
先说说路径分隔符: Unix下的是 /  在Dos下的是 \怎么记:从键盘上来记: U字符就像一个向下的降落的东西,所以找键盘上的下面的那个斜杠。。。。File f = new File("aa\\b.txt");//这里Dos下的还需要转义,因为\在java中是一个用于转义的字符,所以使用Dos格式的时候需要转义。。。总结。。我们就用Unix下的格式吧。。。。。好消息是:java是可以混淆这两种路径分隔符哈。。。File是磁盘文件本身信息的类。。。。所以目录也是磁盘上的一种文件。。。。File f= new File("a");f.isDirectory();//是来判断他是否是一个目录f.lastModified();//是得到文件最后的修改时间,这里返回long,我们要变成我们认识的东西还得使用Date,Date不仅接受String,他还接受所有基本类型f.getParent(); //这个的意思是如果new 的时候是new File("aa\\b.txt");那么会返回aa,如果new File("b.txt") 那么返回null默认情况下new File("/")代表的目录为:System.getProperty("user.dir");形如:D:\program\openeap_devstudio\workspace\ctdmanager,也就是项目的根目录,下面有bin目录new File("/").getAbsolutePath() //返回的是  D:\String s = System.getProperty("user.dir")+"\\WebRoot\\images\\alert.gif";File f = new File(s); //这样可以拿到webRoot下面的东西//这里可以得到 meta-inf下的东西哦。。。。在jsp页面和java页面都可以,当然,因为他们都最后在bin目录下嘛InputStream in = getClass().getResourceAsStream("/META-INF/corp_template.txt" );注意:f.mkdirs();是建立目录,如果f是: C:/zz/1.txt那么用这个命令也会建立一个1.txt的目录File f = new File(root.toString());File dirs = f.getParentFile();if(!dirs.exists()){	dirs.mkdirs();}--------------------------------------------------------------RandomFileAccess类是 随机文件访问类,他提供了众多的文件访问方法如果我们不想从头到尾开始读写文件,那么就要使用这个类。。。比如流媒体文件的应用中,比如断点续传中。。。。。。这个就比流读取方式灵活多了new RandomAccessFile(f,"rw");//对文件以读写方式打开new RandomAccessFile(f,"r");//对文件以只读方式打开String name="aaaa";int age = 23;RandomAccessFile ra = new RandomAccessFile(f,"rw");ra.write(name.getBytes());//写入一个字节数组----这个不好控制,因为你先getBytes()是用操作系统的规范,所以你读出来不知道是多少字符,然后再写入,也就是说你写入的时候不知道是多少字符改:ra.writeChars(name); //这样就写入了这个字符,我们规定name一定为8个字符,因为name是在java中给写入的,所以写入一定是16个字节,而且是用Unicode编码写入对应的读取方式为:String name = "";for(int i=0;i<8;i++){   name +=ra.readChar();}或者:因为我们知道是用Unicode写入,那么读取的时候当然知道怎么读取byte[] buf = new byte[16];int len = ra.read(buf);String s = new String(buf,0,len);ra.write(age);//这是写入一个字节,那么如果这个age大于一个字节,那么会发生丢失,怎么办呢?换下面的这个ra.writeInt(age);//这样会四个字节哈。。。一定会把int完整写入ra.close();Byte[] buf = new Byte[8];RandomAccessFile raf = new RandomAccessFile(f,"rw");raf.skipBytes(12);//第一个员工的信息被跳过,因为String我们用一个buf[8]来接受,也就是说我们规定了name的最大长度为8byte,然后一个int为4byte,所以加起来为12byte....我们对String做了一件事,如果大于8个字符,那么截断,如果少于8个字符,那么补全。。。-----注意啦:如果我们用length来规定长度,他规定的是字符长度,并不等于我们在写入文件的时候是固定的字节长度int retLen = raf.read(buf);//把读到的东西放到buf里面,因为我们知道我们文件的格式,所以就读8个,retLen为读到的长度,如果buf是8位,但是只读到了5为,那么就会返回5下面要把buf转换成String ....要变字符串,当然是new String...一定会有相应的构造方法的,就像如果要把String s = new String(buf,0,retLen);//把buf中从第0个到retLen个字节都拿出来组装成一个字符串raf.seek(0);//跳转到0这个位置,这是绝对定位,也就是跳转到开始。。raf.close();raf.read()//读取1个字节的内容,因为我们明确知道是一个字节,所以返回值就不应该是一个字节的长度信息,这样很累赘,所以他返回的是读取的那个字节的内容raf.readInt();//读取四个字节的整数来返回raf.read(buf);//读取buf长度的信息,返回的是字节长度。。在java中因为是unicode编码,所以中文和英文都占用两个字节的空间,但是在本地操作系统中英文是1个字节,汉字是两个字节s.getBytes();//这个方法是根据本地操作系统来操作的,如果他碰到一个英文只会转换成一个字节,中文会转换成两个字节比如:s = "张三\u0000\u0000\u0000\u0000\u0000\u0000"; s.length()为8,他指的是字符的长度s.getBytes();//这个时候“张三”这两个字转换成了4个字节,而后面的转换成了6个字节,所以一共是10个字节题目1:1、编写一个程序,将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中,a.txt文件中的单词用回车符分隔,b.txt文件中用回车或空格进行分隔。public class MainClass{	public static void main(String[] args) throws Exception{		FileManager a = new FileManager("a.txt",new char[]{'\n'});		FileManager b = new FileManager("b.txt",new char[]{'\n',' '});				FileWriter c = new FileWriter("c.txt");		String aWord = null;		String bWord = null;		while((aWord = a.nextWord()) !=null ){			c.write(aWord + "\n");			bWord = b.nextWord();			if(bWord != null)				c.write(bWord + "\n");		}				while((bWord = b.nextWord()) != null){			c.write(bWord + "\n");		}			c.close();	}	}class FileManager{	String[] words = null;	int pos = 0;	public FileManager(String filename,char[] seperators) throws Exception{		File f = new File(filename);		FileReader reader = new FileReader(f);		char[] buf = new char[(int)f.length()]; //直接把文件全部都load到内存中。。好狠		int len = reader.read(buf);		String results = new String(buf,0,len);		String regex = null;		if(seperators.length >1 ){			regex = "" + seperators[0] + "|" + seperators[1];		}else{			regex = "" + seperators[0];		}		words = results.split(regex);  //把整篇文章导成数组	}		public String nextWord(){		if(pos == words.length)			return null;		return words[pos++]; 	}}2。、编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,并将原来文件的扩展名从.java改为.jad。public class Jad2Java {	public static void main(String[] args) throws Exception {		File srcDir = new File("java");		if(!(srcDir.exists() && srcDir.isDirectory()))				throw new Exception("目录不存在");		File[] files = srcDir.listFiles(			new FilenameFilter(){					public boolean accept(File dir, String name) {						return name.endsWith(".java");					}									}		);				System.out.println(files.length);		File destDir = new File("jad");		if(!destDir.exists()) destDir.mkdir();		for(File f :files){			FileInputStream  fis = new FileInputStream(f);			String destFileName = f.getName().replaceAll("\\.java$", ".jad");			FileOutputStream fos = new FileOutputStream(new File(destDir,destFileName));			copy(fis,fos);			fis.close();			fos.close();		}	}		private static void copy(InputStream ips,OutputStream ops) throws Exception{		int len = 0;		byte[] buf = new byte[1024];		while((len = ips.read(buf)) != -1){			ops.write(buf,0,len);		}	}}

?

  相关解决方案