字节流
//读文件
@Test
public void test() {
File file = new File("D:/java/io/1.txt");
try {
InputStream inputStream = new FileInputStream(file);
byte[] bytes = new byte[1024];
int length = 0;
try {
while ((length = inputStream.read(bytes)) != -1) {
String string = new String(bytes);
System.out.println(string);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("IOException");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("FileNotFoundException");
}
}
//写文件
@Test
public void test2() {
File file = new File("D:/java/io/1.txt");//建立联系
OutputStream outputStream = null;
try {
try {
outputStream = new FileOutputStream(file, true); //选择流对象
String string = "this is a dog \r\n";
byte[] bytes = string.getBytes(); //字符窜转字节数组
outputStream.write(bytes, 0, bytes.length);
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件不存在!!!!!!!!!!!!!!!!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
//文件的读取和写出
@Test
public void fileCopyTest() throws FileNotFoundException, IOException {
File file = new File("D:\\java\\io\\1.txt"); //文件的源头
File file1 = new File("D:\\java\\io\\11.txt"); //目的地
if (file.exists()) {
InputStream inputStream = new FileInputStream(file);
OutputStream outputStream = new FileOutputStream(file1);
byte[] bytes = new byte[1024];
int len = 0;
while (-1 != (len = inputStream.read(bytes))) {
outputStream.write(bytes, 0, len);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
}
//文件读写的方法
/**
* @return void
* @Author zhangfu
* @Description 文件的拷贝
* @Date 8:34 2018/10/29
* @Param [srcPath, destPath]
*/
public static void fileCopy(File srcPath, File destPath) throws FileNotFoundException, IOException {
InputStream inputStream = new FileInputStream(srcPath);
OutputStream outputStream = new FileOutputStream(destPath);
byte[] bytes = new byte[1024];
int len = 0;
while (-1 != (len = inputStream.read(bytes))) {
outputStream.write(bytes, 0, len);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
/**
* @Author zhangfu
* @Description 文件的拷贝
* @Date 9:10 2018/10/29
* @Param [strPath, destPath]
* @return void
*/
public static void fileCopy(String strPath, String destPath) throws IOException {
File file = new File(strPath);
File file1 = new File(destPath);
fileCopy(file, file1);
}
字符流
@Test
public void test() throws FileNotFoundException,IOException{
File file=new File("D:\\java\\1.txt");
Reader reader=new FileReader(file);
char[] chars=new char[12];
int len=0;
while(-1!=(len=reader.read(chars))){
String str=new String(chars,0,len); //字符流转换成字符窜
System.out.println(str);
}
}
@Test
public void test2() throws FileNotFoundException,IOException{
File file=new File("D:\\java\\3.txt");
Writer writer=new FileWriter(file);
String string=" this is name";
writer.write(string);
writer.flush();
}
@Test
public void copyfile() throws FileNotFoundException,IOException{
File file=new File("D:\\java\\IOTest.java");
File file1=new File("D:\\java\\3.txt");
Reader reader=new FileReader(file);
Writer writer=new FileWriter(file1);
char[] chars=new char[1024];
int len=0;
while(-1!=(len=reader.read(chars))){
writer.write(chars,0,len);
}
writer.flush();
}
@Test
public void copyfileBuffer() throws FileNotFoundException,IOException{ //处理流
File file=new File("D:\\java\\IOTest.java");
File file1=new File("D:\\java\\3.txt");
BufferedReader reader=new BufferedReader(new FileReader(file));
BufferedWriter writer=new BufferedWriter(new FileWriter(file1)) ;
String line=null;
while(null!=(line=reader.readLine())){
writer.write(line);
writer.newLine();
}
writer.flush();
}
其他的流
@Test
public void test()throws IOException{
byte[] bytes= this.getBytes("D:\\java\\io\\1.txt");
System.out.println(new String(bytes));
String string="D:\\java\\io\\66666.txt";
File file=new File(string);
this.outFile(bytes,file);
}
//程序------>文件
public static void outFile(byte[] bytes,String string) throws FileNotFoundException,IOException{
File file=new File(string);
InputStream inputStream=new BufferedInputStream(new ByteArrayInputStream(bytes));
OutputStream outputStream=new BufferedOutputStream(new FileOutputStream(file));
int len=0;
while (-1!=(len=inputStream.read(bytes))){
outputStream.write(bytes,0,len);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
//程序-----> 文件
public static void outFile(byte[] bytes,File file) throws FileNotFoundException,IOException{
InputStream inputStream=new BufferedInputStream(new ByteArrayInputStream(bytes));
OutputStream outputStream=new BufferedOutputStream(new FileOutputStream(file));
int len=0;
while(-1!=(len=inputStream.read(bytes))){
outputStream.write(bytes,0,len);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
//文件----->程序(输入)
public static byte[] getBytes(String string) throws FileNotFoundException,IOException{
File file=new File(string);
InputStream inputStream=new BufferedInputStream(new FileInputStream(file));
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
byte[] bytes=new byte[1024];
int len=0;
while (-1!=(len=inputStream.read(bytes))){
byteArrayOutputStream.write(bytes,0,len);
}
bytes= byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
inputStream.close();
return bytes;
}
编码和解码以及字节流和字符流的转换
@Test
public void test() {
String string = "中国";
byte[] bytes = string.getBytes();
//字节数读取不完整造成乱码
System.out.println(new String(bytes, 0, 2));
}
@Test
public void test2() {
//解码 byte--->char
String string = "中国"; //UTF-8
//编码 char---->byte
byte[] bytes = string.getBytes();
System.out.println(new String(bytes));
try {
bytes = string.getBytes("GBK");
//不同回出现乱码
System.out.println(new String(bytes));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("乱码的处理!!!!!!!!!!!!!!!!!!!!!!!");
try {
bytes = string.getBytes("GBK"); //编码
string = new String(bytes, "GBK");
System.out.println(string);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Test
public void test3() {
//指定解码字符集
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(
new BufferedInputStream(
new FileInputStream(
new File("E:/xp/test/Demo03.java"))), "UTF-8")
);
//写出文件 编码
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(
new BufferedOutputStream(
new FileOutputStream(new File("E:/xp/test/encode.java")))));
String info = null;
while (null != (info = br.readLine())) {
bw.write(info);
bw.newLine();
}
bw.flush();
bw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void test4() {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(new FileInputStream(new File("D:\\java\\io\\2112.txt"))), "UTF-8"));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(new File("D:\\java\\io\\211.txt")))));
String string = null;
while (null != (string = bufferedReader.readLine())) {
bufferedWriter.write(string);
bufferedWriter.newLine();
}
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}