字節流
-->
-->
字節輸出流
--
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/level/yin.txt");
FileOutputStream oStream = new FileOutputStream(file);
oStream.write(48);
byte[] b = {65,66,67,68};
oStream.write(b);
oStream.write(b, 0, 3);
oStream.write("hello".getBytes());
oStream.write("world".getBytes());
oStream.close();
}
文件的續寫和換行:
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/level/gs.txt");
FileOutputStream oStream = new FileOutputStream(file, true);
oStream.write("hello\n".getBytes());
oStream.close();
}
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/level/gs.txt");
FileOutputStream oStream = null;
try {
oStream = new FileOutputStream(file);
oStream.write("hello".getBytes());
} catch (FileNotFoundException e) {
throw new RuntimeException("文件找不到");
} catch (IOException e) {
throw new RuntimeException("文件編寫失敗");
} finally {
try {
if (oStream != null) {
oStream.close();
}
} catch (IOException e) {
throw new RuntimeException("關閉失敗");
}
}
}
字節輸入流
字節輸入流(Input) -- 讀文件
InputStream是抽象類不能實例化對象並且是所有輸入流的父類
注意:讀取是一個字節一個字節的讀
讀取文件流程
1.綁定數據源文件
2.使用read方法讀
3.關閉資源
public static void main(String[] args) throws IOException{
File file = new File("/Users/lanou/Desktop/level/znb.txt");
FileInputStream iStream = new FileInputStream(file);
int i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println(i1);
iStream.close();
}
循環讀取:
public static void main(String[] args) throws IOException{
File file = new File("/Users/lanou/Desktop/level/znb.txt");
FileInputStream iStream = new FileInputStream(file);
int num = 0;
while ((num=iStream.read()) != -1) {
System.out.println((char)num);
}
iStream.close();
}
注意:read方法每調用一次就讀取一個字節,循環的時候read方法只能出現一次
使用字節數組讀取文件:
public static void main(String[] args) throws IOException{
File file = new File("/Users/lanou/Desktop/level/znb.txt");
FileInputStream iStream = new FileInputStream(file);
byte[] b = new byte[2];
int i1 = iStream.read(b);
System.out.println(i1);
System.out.println(new String(b));
i1 = iStream.read(b);
System.out.println(i1);
System.out.println(new String(b));
i1 = iStream.read(b);
System.out.println(i1);
System.out.println(new String(b));
i1 = iStream.read(b);
System.out.println(i1);
System.out.println(new String(b));
iStream.close();
}
字節的輸入輸出流練習
使用字節的輸入輸出流進行文件的複製:
public class Demo01 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
FileInputStream iStream = null;
FileOutputStream oStream = null;
try {
iStream = new FileInputStream("/Users/lanou/Desktop/level/9.jpg");
oStream = new FileOutputStream("/Users/lanou/Desktop/level/10.jpg");
byte[] b = new byte[1024];
int num = 0;
while ((num = iStream.read(b)) != -1) {
oStream.write(b, 0, num);
}
} catch (FileNotFoundException e) {
throw new RuntimeException("文件找不到");
} catch (IOException e) {
throw new RuntimeException("文件複製失敗");
} finally {
try {
if (iStream != null) {
iStream.close();
}
} catch (IOException e) {
throw new RuntimeException("關閉資源失敗");
} finally {
try {
if (oStream != null) {
oStream.close();
}
} catch (IOException e) {
throw new RuntimeException("關閉資源失敗");
}
}
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
將一個文件夾複製到另一個文件夾:
public class Demo02 {
public static void main(String[] args) throws IOException {
File file1 = new File("/Users/lanou/Desktop/level");
File file2 = new File("/Users/lanou/Desktop/XTest");
copyFile(file1, file2);
}
public static void copyFile(File file1,File file2) throws IOException {
File newDir = new File(file2, file1.getName());
newDir.mkdir();
File[] listFiles = file1.listFiles();
for (File file3 : listFiles) {
if (file3.isDirectory()) {
copyFile(file3, newDir);
} else {
FileInputStream iStream = new FileInputStream(file3);
File tempFile = new File(newDir, file3.getName());
FileOutputStream oStream = new FileOutputStream(tempFile);
byte[] b = new byte[1024];
int num = 0;
while ((num = iStream.read(b)) != -1) {
oStream.write(b, 0, num);
}
iStream.close();
oStream.close();
}
}
}
}
將一個文件夾下的txt文件複製到另一個文件夾:
public class Demo03 {
public static void main(String[] args) throws IOException {
File file1 = new File("/Users/lanou/Desktop/level");
File file2 = new File("/Users/lanou/Desktop/XTest");
getTXTFile(file1, file2);
}
public static void getTXTFile(File file1,File file2) throws IOException {
File[] listFiles = file1.listFiles(new FilterTXT());
for (File file : listFiles) {
if (file.isFile()) {
FileInputStream iStream = new FileInputStream(file);
File tempFile = new File(file2, file.getName());
FileOutputStream oStream = new FileOutputStream(tempFile);
byte[] b = new byte[1024];
int num = 0;
while ((num = iStream.read(b)) != -1) {
oStream.write(b, 0, num);
}
iStream.close();
oStream.close();
} else {
getTXTFile(file, file2);
}
}
}
}
class FilterTXT implements FileFilter {
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()) {
return true;
}
return pathname.getName().endsWith("txt");
}
}