天天看点

文件功能学习笔记

/**
 * 文件操作——File
 * @author cdw
 *
 */
/**
 *java.io.File用于表示文件(目录),也就是说程序员可以通过File类在程序中操作硬盘上的文件和
 * 目录。File类只用于表示文件(目录)的信息(名称,大小等),不能对文件的内容进行访问
 */
/**构造方法:File(String pathname)通过将给定路径名字符串转换成抽象路径名来创建一个新的File实例。*/

import java.io.File;
import java.io.IOException;

import org.junit.Test;

public class test01 {

@Test
public void testFile() {
	File file = new File("demo" + File.separator + "HelloWORLD.txt");
	System.out.println(file);
}
//File(File parent,String child)根据parent抽象路径名个child路径名字符串创建一个新的File实例
@Test
public void testFile2() {
	File parent = new File("demo");
	File file = new File(parent,"HelloWorld.txt");
	System.out.println(file);
}
/**isFile()方法:File的isFile方法用于判断当前File对象所表示的是否为一个文件。
 * boolean ifFile()返回值:当前File对象所表示的是一个文件时返回true
 */
@Test
public void testIsFile() {
	File file = new File("demo" + File.separator + "HelloWorld.txt");
	System.out.println(file + "是否是一个文件" + file.isFile());
}
/**File的length方法用于返回此抽象路径名表示的文件的长度(占用的字节量)
 * ——long length()返回值:当前File对象所表示的文件所占用的字节量
 */
@Test
public void testLength() {
	File file = new File("demo" + File.separator + "HelloWorld.txt");
	System.out.println(file + "占用字节量:" + file.length());
}
/**exists()方法
 * File的exists方法用于测试此抽象路径名称标识的文件或目录是否存在
 * -boolean exists()返回值,若该File表示的文件或目录存在则返回true,否则返回false
 */
/**createNewFile()方法
 * File的createNewFile方法用于当且仅当不存在具有此抽象路径名指定的名称的文件时,创建由此抽象路径名
 * 指定的一个新的空文件。
 * boolean creatNEWFile()返回值:如果指定的文件不存在并成功创建,则返回true;如果指定的文件已经存在
 * 则返回false
 * @throws IOException 
 */
@Test
public void testCreateNewFile() throws IOException {
	File file = new File("D:\\" + File.separator + "Hello.txt");
	//若不存在,就创建该文件
	if(!file.exists()) {
		file.createNewFile();
	}
}
/**File的delete方法用于删除此抽象路径名表示的文件或目录
 * boolean delete()返回值:当且仅当成功删除文件或目录时,返回true;否则返回false。
 * 需要注意的是,若此File对象所表示的是一个目录时。在删除时需要保证此为空目录才可以成功删除。
 */
@Test
public void testFeleteFile() {
	File file = new File("D:\\" + File.separator + "Hello.txt");
	file.delete();
}
/**isDirectory()方法
 * File的isDirectory方法用于判断当前File表示的是否为一个目录
 * -boolean isDirectory()返回值:当File对象表示的是一个目录时返回true;否则返回false
 */
@Test
public void testIsDirectory() {
	File file = new File("demo");
	System.out.println(file + "是否是同一个目录" + file.isDirectory());
}
/**mkdir()方法
 * File 的mkdir方法用于创建此抽象路经名指定的目录
 * ——boolean mkdir()返回值,当且仅当已创建目录时,返回true;否则返回false
 */
@Test
public void testMkdir() {
	File dir =new File("myDir");
	dir.mkdir();
}
/**mkdirs()方法
 * File的mkdirs方法用于创建此抽象路径名指定的目录,包括所有必须但不存在的父目录。注意,此操作失败
 * 时也可能已经成功创建了一部分必须的父目录。
 * ——boolean mkdirs()返回值:当且仅当已创建目录以及所有必须的父目录时,返回true,否则返回false
 */
public void testMkDirs() {
	File dir = new File("a" + File.separator + "b" + File.separator + "c");
	dir.mkdirs();
}
/**listFiles()方法
 * File的listFiles方法用于返回一个抽象路径名数组,这些路径名表示此抽象路劲名标识的目录
 * 中的子项(文件或目录)
 * ——File[].listFiles()返回值:抽象路径名数组,这些路径名表示此抽象路径名表示的目录中
 * 的文件或目录。如果目录为空,那么数组也将为空。如果抽象路径名不表示一个目录,或者发生
 * I/O错误,则返回NULL
 */
public void testListFiles() {
	File dir = new File(".");
	File[] subs = dir.listFiles();
	for(File sub: subs) {
		System.out.println(sub);
	}
}
}
           
import java.io.RandomAccessFile;


import org.junit.Test;

public class test01{
/**
 * Java提供了一个可以对文件随机访问的操作,访问包括读和写操作,该类名为RandomAccessFile
 * 该类的读写是基于指针的操作
 * RandomAccessFile在对文件进行随机访问操作时有两个模式,分别为只读模式(只读取文件数据);
 * 和读写模式(对文件数据进行读写)
 */
/**
 * 在创建RandomAccessFile是,其提供的构造方法要求我们传入访问模式:
 * ————RandomAccessFile(File file,String mode)
 * ————RandomAccessFile(String filename,String mode)
 * 其中构造方法的第一个参数是需要访问的文件,而第二个参数则是访问模式
 * ————“r”:表示对该文件的访问是只读的
 * ————“rw”:表示对该文件的访问是读写模式的
 */
/**RandomAccessFile提供了一个可以向文件中写出字节的方法:
 * ————void write(int d)该方法会根据当前指针所在文职出写入一个字节,是将参数int的"低8位"写出
 * @throws Exception 
 */
	@Test
	public void testWrite() throws Exception {
		RandomAccessFile raf = new RandomAccessFile("d:/Hello.txt", "rw");
		//写出一个字节,写的是int值的低8位
		raf.write(1);
		raf.close();
	}
/**read()方法
 * RandomAccessFile提供了一个可以向文件中读取字节的方法:
 * ————int read()
 * 该方法会从文件中读取一个byte(8位)填充到int的低八位,高24位为0,返回值范围整数:0·255
 * 如果返回-1表示读取到了文件末尾!每天读取后自动移动文件指针,准备下次读取
 */
	@Test
	public void testRead() throws Exception{
		RandomAccessFile raf = new RandomAccessFile("d:/Hello.txt","rw");
		//读取一个字节
		int d = raf.read();
		System.out.println(d);
		raf.close();
	}
/**write(byte[] b)方法
 * ————void write(byte[] d)该方法会根据当前指针所在位置处连续写出给定数组中的所有字节。
 * 与该方法类似的还有一个常用方法:
 * ————void write(byte[] d)该方法会根据当前指针所在位置处连续写出给定数组中的所有字节
 * 与该类似相似的还有一个常用方法
 * ————void write(byte[] d,int offset,int len)
 * 该方法会根据当前指针所在位置出两虚写出给定数组中的部分字节,这个部分是从数组的offset
 * 处开始,连续len个字节
 * @throws Exception 
 */
	@Test
	public void testWriteByteArray() throws Exception {
		RandomAccessFile raf = new RandomAccessFile("d:/hello.txt","rw");
		byte[] buf = "hello world".getBytes();
		raf.write(buf);
		raf.close();
	}
/**read(byte[] b)方法
 * RandomAccessFile提供了一个可以向文件中批量读取字节的方法:
 * ————int read(byte[] b)
 * 该方法会从指针位置出尝试最多读取给定数组的总长度的字节量,并从给定的字节数组第一个位置开始,
 * 将读取到的字节顺序存放至数组中,返回值威士忌读取到的字节量。
 * @throws Exception 
 */
	@Test
	public void testReadByteArray() throws Exception {
	RandomAccessFile raf = new RandomAccessFile("d:/Hello.txt","r");
	//创建10字节数组
	byte[] buf = new byte[10];
	//尝试读取10个字节存入数组,返回值为读取的字节量
	int len = raf.read(buf);
	System.out.println("读取到了:" + len + "个元素");
	System.out.println(new String(buf));
	raf.close();
	}
/**close()方法
 * RandomAccessFile在对文件访问的操作全部结束后,要调用close()方法来释放与其关联的所有系统资源
 * ————void close()
 * RandomAccessFile raf = new RandomAccessFile(file,"rw")
 */
 //raf.close();访问完毕后要关闭以释放资源

}