首先介绍File类:
我们直接上代码:
package com.learn.chap10.sec02; import java.io.File;
import java.io.IOException; /**
* 测试创建目录和文件
* @author Administrator
*
*/
public class TestFile {
public static void main(String[] args) throws IOException {
File file=new File("E://java创建目录");
boolean b = file.mkdir();
if(b){
System.out.println("创建目录成功!");
file=new File("E://java创建目录//test.txt");
boolean m = file.createNewFile();
if(m){
System.out.println("创建文件成功!");
}else{
System.out.println("创建文件失败!");
}
}else {
System.out.println("创建目录失败!");
}
}
}
package com.learn.chap10.sec02; import java.io.File; public class TestFile1 {
public static void main(String[] args) {
File file = new File("E://java创建目录//test.txt");
if(file.exists()){ // 假如文件存在
boolean a = file.delete(); // 删除文件
if(a){
System.out.println("文件删除成功!");
}else {
System.out.println("文件删除失败!");
}
} file = new File("E://java创建目录");
if(file.exists()){ // 假如目录存在
boolean m = file.delete(); // 删除目录(注意:delete只能删除空目录)
if(m){
System.out.println("目录删除成功!");
}else {
System.out.println("目录删除失败!");
}
} }
}
package com.learn.chap10.sec02; import java.io.File; public class TestFile2 {
public static void listFile(File file){
if(file != null){
if(file.isDirectory()){
File[] a = file.listFiles(); // 遍历目录
if(a != null){
for (int i = 0; i < a.length; i++) {
listFile(a[i]); // 递归调用
}
}
}else {
System.out.println(file);
}
}
} public static void main(String[] args) {
listFile(new File("E://mywamp//apache//conf//original"));
}
}
再介绍字节流:
上下代码:
package com.learn.chap10.sec03; import java.io.File;
import java.io.FileInputStream; import java.io.InputStream;
/**
* 字节流---文件输入流测试(流入内存:从磁盘读取至内存)
* 输入、输出是针对内存 (举例: 输入流 即数据从硬盘流向内存,也可说是从硬盘读取数据至内存)
* 读取、写入是针对磁盘
* @author Administrator
*
*/
public class Demo1 {
public static void main(String[] args) throws Exception {
File file=new File("E://测试文件.txt");
InputStream inputStream = new FileInputStream(file);
InputStream inputStream1 = new FileInputStream(file);
InputStream inputStream2 = new FileInputStream(file);
// 读取数据方法一
byte[] b=new byte[1024];
inputStream.read(b); // 读取数据方法二
int fileLength1 = (int)file.length();
byte[] b1=new byte[fileLength1];
inputStream1.read(b1); // 读取数据方法三
int fileLength = (int)file.length();
byte[] m=new byte[fileLength];
int temp =0;
int len=0;
while((temp=inputStream2.read()) != -1){
m[len++] = (byte)temp;
} inputStream.close(); // 关闭输入流
inputStream1.close(); // 关闭输入流
inputStream2.close(); // 关闭输入流
System.out.println("读取的内容为:"+new String(b));
System.out.println("读取的内容为:"+new String(b1));
System.out.println("读取的内容为:"+new String(m));
} }
1 package com.learn.chap10.sec03; import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
/**
* 字节流---文件输出流测试
* @author Administrator
*
*/
public class Demo2 {
public static void main(String[] args) throws Exception {
File file=new File("E://测试文件.txt");
OutputStream out = new FileOutputStream(file,true);// 加true 表示追加
String string="呵呵呵呵12";
byte[] b=string.getBytes();
out.write(b);
out.close();
}
}
package com.learn.chap10.sec03; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; /**
* 字节流---缓冲流和非缓冲流比较
* @author Administrator
*
*/
public class Demo3 {
/**
* 非缓存
* @throws Exception
*/
public static void stream() throws Exception{
InputStream inputStream=new FileInputStream("E://测试文件1.txt");
OutputStream outputStream=new FileOutputStream("E://测试文件2.txt");
int b=0;
long start_time = System.currentTimeMillis(); // 开始时间
while((b=inputStream.read()) != -1){
outputStream.write(b);
}
inputStream.close();
outputStream.close();
long end_time = System.currentTimeMillis(); // 结束时间
System.out.println("非缓存花费时间:"+(end_time-start_time)); } /**
* 缓冲流 节省时间 性能优
* @throws Exception
*/
public static void buffer_stream() throws Exception {
// 定义了一个带缓冲的字节输入流
BufferedInputStream bInputStream = new BufferedInputStream(new FileInputStream("E://测试文件1.txt"));
// 定义了一个带缓冲的字节输出流
BufferedOutputStream bOutputStream = new BufferedOutputStream(new FileOutputStream("E://测试文件2.txt"));
int b=0;
long start_time = System.currentTimeMillis(); // 开始时间
while((b=bInputStream.read()) != -1){
bOutputStream.write(b);
}
bInputStream.close();
bOutputStream.close();
long end_time = System.currentTimeMillis(); // 结束时间
System.out.println("缓存花费时间:"+(end_time-start_time));
} public static void main(String[] args) throws Exception {
stream();
buffer_stream();
}
}
再介绍下字符流:
上下代码:
package com.learn.chap10.sec04; import java.io.File;
import java.io.FileReader;
import java.io.Reader;
/**
* 字符流----输入流
* @author Administrator
*
*/
public class Demo1 {
public static void main(String[] args) throws Exception {
File file=new File("E://测试文件1.txt");
Reader reader=new FileReader(file);
char[] c=new char[1024]; // 字符数组
// 方法一
int len=reader.read(c);
// 方法二
/*int temp=0;
int len=0;
while((temp=reader.read()) != -1){
c[len++] = (char)temp;
}*/
reader.close();
System.out.println("读取内容是:"+new String(c,0,len)); }
}
运行结果:
读取内容是:gffdhgfhgjhgjhgjhk
kukhjkhjk gfhfghgjhjhkjhkjhkjhkjhkj
package com.learn.chap10.sec04; import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
/**
* 字符流----输出流
* @author Administrator
*
*/
public class Demo2 {
public static void main(String[] args) throws Exception {
File file=new File("E://测试文件1.txt");
Writer outWriter = new FileWriter(file,true);
String string="adffdsf";
outWriter.write(string); // 将字符串写入输出流
outWriter.close(); // 关闭数据流
}
}