1. 程式人生 > >用字符流,字節流復制文本文件或者圖片

用字符流,字節流復制文本文件或者圖片

寫入 一個 exception dex red day put bytes flush

--IO流(2)----

(1)普通字符流,復制文本文件,一次復制一個字符

public static void main(String[] args) throws FileNotFoundException {
try(FileReader reader = new FileReader("Day26_IO02\\a.txt");
FileWriter writer = new FileWriter("Day26_IO02\\b.txt");
){

int read = reader.read();
while ((read = reader.read()) != -1){
writer.write(read);
}
} catch (IOException e) {
e.printStackTrace();
}
}

(2)普通字符流,復制文本文件,一次復制一個字符數組
public static void main(String[] args) throws FileNotFoundException {
try(FileReader reader = new FileReader("Day26_IO02\\a.txt");
FileWriter writer = new FileWriter("Day26_IO02\\b.txt");
){

char[] chars = new char[1024];
int read;
while ((read = reader.read(chars)) != -1){
String s = new String(chars,0,read);
writer.write(s);
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}

(3)高效字符流,復制文本文件,一次復制一個字符
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("Day26_IO02\\a.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("Day26_IO02\\b.txt"))
) {

int read = reader.read();
while ((read = reader.read()) != -1) {
writer.write(read);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
(4)高效字符流,復制文本文件,一次復制一個字符數組
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("Day26_IO02\\a.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("Day26_IO02\\b.txt"))
) {

char[] chars = new char[1024];
int read;
while ((read = reader.read(chars)) != -1){
String s = new String(chars,0,read);
writer.write(s);
writer.flush();
}
} catch (
FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

(5)高效字符流,復制文本文件,一次復制一行數據
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("Day26_IO02\\a.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("Day26_IO02\\b.txt"))
) {

String s;
while ((s = reader.readLine()) != null){ //一次讀取一行數據
writer.write(s); //一次寫入一行數據
writer.newLine();//換行
}
} catch (
FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
(6)普通字節流,復制圖片,一次復制一個字節
public static void main(String[] args) throws FileNotFoundException {
try(InputStream inputStream = new FileInputStream("Day26_IO02\\1.jpg");
OutputStream outputStream = new FileOutputStream("Day26_IO02\\2.jpg");
){
int read;
while ((read = inputStream.read()) != -1){
outputStream.write(read);
}
} catch (IOException e) {
e.printStackTrace();
}
}
(7)普通字節流,復制圖片,一次復制一個字節數組
public static void main(String[] args) throws FileNotFoundException {
try(InputStream inputStream = new FileInputStream("Day26_IO02\\1.jpg");
OutputStream outputStream = new FileOutputStream("Day26_IO02\\2.jpg");
){

byte[] bytes = new byte[1024];
int read;
while ((read = inputStream.read(bytes)) != -1){
outputStream.write(bytes,0,read);
}
} catch (IOException e) {
e.printStackTrace();
}
}
(8)高效字節流,復制圖片,一次復制一個字節
public static void main(String[] args) {
try(BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("Day26_IO02\\1.jpg"));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("Day26_IO02\\2.jpg"))
){
int read;
while ((read = inputStream.read()) != -1){
outputStream.write(read);
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
(9)高效字節流,復制圖片,一次復制一個字節數組
public static void main(String[] args) {
try(BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("Day26_IO02\\1.jpg"));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("Day26_IO02\\2.jpg"))
){
byte[] bytes = new byte[1024];
int read;
while ((read = inputStream.read(bytes)) != -1){
outputStream.write(bytes,0,read);
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

用字符流,字節流復制文本文件或者圖片