1.IO流主要内容:
2.File类:
import java.io.File;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
File f = new File("D:\\test\\abc\\tt1.txt");//对象f为tt.txt
// File f1 = new File("D:\\test", "abc\\tt.txt");//对象f也为tt.txt
// java中\为转义符,\\和/才是文件分隔符
// File f2 = new File("D:" + File.separator + "test\\abc\\tt.txt");
// System.out.println(f.getName());
// System.out.println(f.getPath());
// System.out.println(f.getAbsolutePath());
// System.out.println(f.getParent());
// f.renameTo(new File("D:\\test\\abc\\tt1.txt"));
// System.out.println(f.exists());
// System.out.println(f.canRead());
// System.out.println(f.canWrite());
// System.out.println(f.isFile());//文件
// System.out.println(f.isDirectory());//目录
// System.out.println(f.lastModified());//最后修改时间
// System.out.println(f.length());//文件长度,单位字节数
// File f3 = new File("D:\\test\\abc\\tt2.txt");
//
// if(!f3.exists()) {
// try {
// f3.createNewFile();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// System.out.println(f3.exists());
// f3.delete();
// System.out.println(f3.exists());
// f3.mkdir();
}
}
3.file类递归遍历文件:
import java.io.File;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
File f = new File("D:\\test");
new Test().test(f);
}
/**
* 递归遍历文件
* @param file
*/
public void test(File file) {
if(file.isFile()) {
System.out.println(file.getAbsolutePath() + " 是文件");
}else {
System.out.println(file.getAbsolutePath() + " 是文件夹");
//如果是文件夹,在这个文件夹里则可能有子文件夹或文件
File[] fs = file.listFiles();
if(fs != null && fs.length > 0) {
for (File ff : fs) {
// if(ff.isFile()) {
// System.out.println(file.getAbsolutePath() + " 是文件");
// }else {
// System.out.println();
// }
test(ff);
}
}
}
}
}
4.IO与IO流体系:
5.文件字节流:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Test1 {
public static void main(String[] args) {
// try {
// FileInputStream in = new FileInputStream("D:\\test\\abc\\tt1.txt");
//
// byte[] b = new byte[10];//设置一个比特数组,接收读取的文件内容
//
// int len = 0;//读取数据的长度
//
in.read(b);//返回读取的数据的长度,如果读取完成,则返回-1
// while((len = in.read(b))!=-1) {
// System.out.println(new String(b,0,len));
// }
// System.out.println(new String(b));
//
// in.close();//流在使用完之后一定要关闭
// } catch (Exception e) {
// e.printStackTrace();
// }
// Test1.testFileInputStream();//文件字节的输入流
Test1.testFileOutputStream();
}
public static void testFileInputStream(){
}
public static void testFileOutputStream() {
try {
FileOutputStream out = new FileOutputStream("D:\\test\\abc\\tt2.txt");
String str = "asdasndl";
out.write(str.getBytes());//数据写入内存
out.flush();//把内存中的数据刷写到硬盘
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
6.文件字节流练习:
public static void copyFile(){
try {
FileInputStream in = new FileInputStream("D:\\test\\abc\\tt1.txt");
FileOutputStream out = new FileOutputStream("D:\\test\\abc\\tt2.txt");
byte[] b = new byte[100];
int len = 0;
while((len = in.read(b)) != -1) {
out.write(b,0,len);
}
out.flush();
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
7.文件字符流:
import java.io.FileNotFoundException;
import java.io.FileReader;
public class Test2 {
public static void main(String[] args) {
Test2.testFileReader("D:\\\\test\\\\abc\\\\tt1.txt");
}
/**
* 文件字符输入流
* @param inPath
*/
public static void testFileReader(String inPath) {
try {
FileReader fr = new FileReader(inPath);//创建输入的对象
char[] c = new char[10];//临时字符数组
int len = 0;//定义一个输入流的读取长度
while((len = fr.read(c)) != -1) {
System.out.println(new String(c, 0, len));
}
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}