字节输入流:
package zz.itheima.byte_char_stream;
import java.io.FileInputStream;
public class TestFileInputStream {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("demo.txt");
System.out.println("**********************");
byte[] temp = new byte[1024];
fis.read(temp);
System.out.println(new String(temp).trim());
} catch (Exception e) {
e.printStackTrace();
}
}
}
字节输出流:
package zz.itheima.byte_char_stream;
import java.io.FileOutputStream;
public class TestFileOutputStream {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("demo.txt");
byte[] val = {97,98,13,99,'v'};
fos.write(val);
byte[] hello = {'h','e','l','l','o'};
fos.write(hello);
String s = "你好!";
fos.write(s.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
}
字符输入(读文件)流:
package zz.itheima.byte_char_stream;
import java.io.File;
import java.io.FileReader;
public class TestFileReader {
public static void main(String[] args) {
try {
File f = new File("demo.txt");
FileReader fr = new FileReader(f);
char[] temp = new char[1024];
while (true) {
int res = fr.read(temp);
if (res==-1) {
break;
}
System.out.println(new String(temp));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
字符输出(写文件)流:
package zz.itheima.byte_char_stream;
import java.io.FileWriter;
import java.io.IOException;
public class TestFileWrite {
public static void main(String[] args) {
try {
FileWriter fw=new FileWriter("demo.txt");
fw.write("wor\nld");
char[] temp={'h','e','l','l','o'};
fw.write(temp);
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符流的缓冲区:
package zz.itheima.byte_char_stream;
import java.io.FileWriter;
public class TestHuanChong {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("demo.txt");
for (int i = 0; i < 1024; i++) {
fw.write("a");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
字节数组大小:
package zz.itheima.byte_char_stream;
import java.io.File;
import java.io.FileInputStream;
public class TestByteArrayLength {
public static void main(String[] args) {
try {
File f = new File("demo.txt");
FileInputStream fis = new FileInputStream(f);
byte[] temp = new byte[(char)f.length()];
fis.read(temp);
String val = new String(temp);
System.out.println(val);
} catch (Exception e) {
e.printStackTrace();
}
}
}
每次读取内容多少不同的效率:
package zz.itheima.byte_char_stream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestEfficiency {
public static void copy1()
{
long begin=System.currentTimeMillis();
try {
File f=new File("E:\\Oracle的钢铁侠植入广告.mov");
FileInputStream fis=new FileInputStream(f);
FileOutputStream fos=new FileOutputStream("d:\\1.mov");
byte[] temp=new byte[1024*100];
while(true)
{
int res=fis.read(temp);
if(res==-1)
break;
fos.write(temp,0,res);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
long end=System.currentTimeMillis();
System.out.println(end-begin);
}
public static void copy2()
{
long begin=System.currentTimeMillis();
try {
File f=new File("E:\\Oracle的钢铁侠植入广告.mov");
FileInputStream fis=new FileInputStream(f);
FileOutputStream fos=new FileOutputStream("d:\\1.mov");
while(true)
{
int res=fis.read();
if(res==-1)
break;
fos.write(res);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
long end=System.currentTimeMillis();
System.out.println(end-begin);
}
public static void main(String[] args) {
copy2();
}
}
复制文件:
package zz.itheima.byte_char_stream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
public static void copy1()
{
try {
File f=new File("E:\\1.jpg");
FileInputStream fis=new FileInputStream(f);
byte[] temp=new byte[(int)f.length()];
fis.read(temp);
FileOutputStream fos=new FileOutputStream("d:\\1.jpg");
fos.write(temp);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copy2()
{
try {
File f=new File("E:\\1.jpg");
FileInputStream fis=new FileInputStream(f);
FileOutputStream fos=new FileOutputStream("d:\\1.jpg");
byte[] temp=new byte[1024*10];
while(true)
{
int res=fis.read(temp);
if(res==-1)
break;
System.out.println(res);
fos.write(temp,0,res);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
copy1();
}
}
复制字符串文件:
package zz.itheima.byte_char_stream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyStringFile {
public static void copy1() {
FileReader fr = null;
FileWriter fw = null;
try {
File f = new File("E:\\1.jpg");
fr = new FileReader(f);
char[] temp = new char[(int) f.length()];
fr.read(temp);
fw = new FileWriter("e:\\1.jpg");
fw.write(temp);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void copy2(){
FileReader fr = null;
FileWriter fw = null;
try {
File f = new File("demo.txt");
fr = new FileReader(f);
fw = new FileWriter("e:\\2.txt");
char[] temp = new char[1024];
while (true) {
int res=fr.read(temp);
if(res==-1)
break;
else {
fw.write(temp,0,res);
}
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
fr.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
}
}
例子一:
package zz.itheima.byte_char_stream;
import java.io.FileOutputStream;
import java.util.Scanner;
public class Demo1 {
public static void method1() throws Exception{
Scanner sc = new Scanner(System.in);
FileOutputStream fos = new FileOutputStream("E:\\demo.txt");
String flag ;
do {
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
fos.write(name.getBytes());
fos.write(13);
fos.write(10);
System.out.println("是否继续(y/n)");
flag = sc.nextLine();
} while (flag.equalsIgnoreCase("y"));
fos.close();
}
public static void method2() throws Exception {
Scanner sc = new Scanner(System.in);
FileOutputStream fos = new FileOutputStream("e:\\demo.txt");
String flag;
String all="";
do {
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
all=all+name+"\r\n";
System.out.println("是否继续(y/n)");
flag = sc.nextLine();
} while (flag.equalsIgnoreCase("y"));
System.out.println(all);
fos.write(all.getBytes());
fos.close();
}
public static void main(String[] args)throws Exception {
method2();
}
}
例子二:
package zz.itheima.byte_char_stream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Demo2 {
public static void copy1(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File f = new File("E:\\1.jpg");
fis = new FileInputStream(f);
byte[] temp = new byte[(int)f.length()];
fis.read(temp);
fos = new FileOutputStream("E:\\2.jpg");
fos.write(temp);
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
fis.close();
fos.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
public static void copy2(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File f = new File("E:\\1.jpg");
fis = new FileInputStream(f);
fos = new FileOutputStream("E:\\3.jpg");
byte[] temp = new byte[10];
while (true) {
int res = fis.read(temp);
if (res==-1) {
break;
}else {
fos.write(temp, 0, res);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
fis.close();
fos.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
public static void main(String[] args) {
copy2();
}
}
例子三:
package zz.itheima.byte_char_stream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("demo.txt");
} catch (IOException e1) {
e1.printStackTrace();
}
Scanner sc = new Scanner(System.in);
int n = 1;
String name = "";
try {
do {
System.out.println("请输入第" + n + "个学生的姓名:");
name = sc.nextLine();
fw.write(name + "\n");
if (n % 3==0) {
fw.flush();
}
n++;
} while (!name.equalsIgnoreCase("quit"));
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}