zl-programmer

读文档中的内容 FileInputStream

package com.app;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TestFileInputStream {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestFileInputStream a1=new TestFileInputStream();
 
//电脑D盘中的abc.txt文档
String filePath="D:/abc.txt";
String result=a1.readFile(filePath);
System.out.println(result);
}
 
/**
* 读取指定文件的内容
* @param filePath : 文件的路径
* @return 返回的结果
*/
public String readFile(String filePath){
FileInputStream fis=null;
String result =null;
try {
// 根据path路径实例化一个输入流的对象
fis=new FileInputStream(filePath);
//2. 返回这个输入流中可以被读的剩下的bytes字节的估计值;
int size=fis.available();
//3. 根据输入流中的字节数创建byte数组;
byte[] array =new byte[size];
//4.把数据读取到数组中;
fis.read(array);
//5.根据获取到的Byte数组新建一个字符串,然后输出;
result=new String(array);
 
 
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if ( fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
 
 
return result;
 
}
}
 
 
将程序内容写入文档 FileOutputStream
package com.app;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestFileOutputStream {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestFileOutputStream a2=new TestFileOutputStream();
//电脑d盘中的abc.txt 文档
String filePath="D:/abc1.txt";
//要写入得内容
String content="今天是2019年1月16日,天气很好!";
a2.writeFile(filePath,content);
}
 
/**
* 根据文件路径创建输出流
* @param filePath : 文件的路径
* @param content : 需要写入的内容
*/
public void writeFile(String filePath,String content){
FileOutputStream fos=null;
 
try {
//1、根据文件路径创建输出流
fos=new FileOutputStream(filePath);
//2、把string转换为byte数组;
byte[] array=content.getBytes();
//3、把byte数组输出;
fos.write(array);
 
 
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if ( fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
 
 
实现文件复制
package com.app;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestFileCopy {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestFileCopy a3=new TestFileCopy();
byte[] content=a3.readFile("C:/Users/Administrator/Desktop/QQ截图20190109165132.png");
String filePath="D:/copy01.png";
a3.writeFile(filePath,content);
}
 
public byte[] readFile(String filePath){
FileInputStream fis=null;
FileOutputStream fos=null;
String result=null;
byte[] array=null;
try {
//读
fis=new FileInputStream(filePath);
int size=fis.available();
array =new byte[size];
fis.read(array);
result=new String(array);
 
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return array;
}
 
public void writeFile(String filePath,byte[] content){
FileOutputStream fos=null;
try {
fos=new FileOutputStream(filePath);
//byte[] array=content.getBytes();
fos.write(content);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
 

分类:

技术点:

相关文章: