JAVA字节流
学了一暑假的JAVA竟然一篇博客没有写,罪过,罪过,今天正式重启了 lalala
文件的打开与创建
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
File dir = new File("tempFile");//打开文件的路径
if(!dir.exists()){ //如果文件不存在,创建新的文件
dir.mkdir();
}
File file = new File(dir, "file.txt");
FileOutputStream fos = new FileOutputStream(file);
byte[] date = "写入的数据".getBytes();
fos.write(date);
fos.close();
}
}
文件的读出
public class Demo1 {
@Test
public void testDemo() throws IOException {
File file = new File("tempFile\\file.txt");
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[3];
int len = 0;
while((len = fis.read(buf)) != -1){
System.out.print(new String(buf, 0, len));
}
}
}
文件目录的递归遍历
import org.junit.Test;
import java.io.File;
/**
* Created by SunMing on 2016/8/31.
*/
public class FileList {
@Test
public void FileList(){
File dir = new File("D:\\JarDate");
ListAll(dir);
}
public static void ListAll(File dir){
File[] files = dir.listFiles();
for(File file: files) {
if (file.isDirectory()) {
ListAll(file);
}else{
System.out.println(file.getName());
}
}
}
}
文件目录的队列遍历