1个回答
展开全部
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class StreamTest {
public static void main(String[] args) {
StreamTest st = new StreamTest();
String writeStr = "Hello World!\r\n你好!";
String fileName = "outFile.txt";
st.OutputTest(fileName,writeStr);
st.InputTest(fileName);
}
//字节-读
private void InputTest(String fileName) {
File f = createFile(fileName);
FileInputStream fis;
byte[] b = new byte[100];
try {
System.out.println("创建输入流...");
fis = new FileInputStream(f);
System.out.println("创建输入流完成");
System.out.println("开始读取...");
fis.read(b);
System.out.println("读取完成");
String str = new String(b);
System.out.println("读取内容输出:\n"+str);
fis.close();
}catch(FileNotFoundException e) {
System.out.println("文件没有找到");
}catch(IOException e) {
System.out.println("读取失败");
}
}
//字节-写
private void OutputTest(String fileName,String text) {
File f = createFile(fileName);
FileOutputStream fos;
try{
System.out.println("创建输出流...");
fos = new FileOutputStream(f);
System.out.println("创建输出流完成");
byte[] testBArray = text.getBytes();
System.out.println("开始写数据...");
fos.write(testBArray);
fos.flush();
System.out.println("写数据完成");
fos.close();
}catch(FileNotFoundException e) {
System.out.println("文件没有找到");
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
//创建文件
private File createFile(String fileName) {
File f = new File(fileName);
if(!f.exists()) {
System.out.println("文件不存在");
try{
System.out.println("创建文件...");
f.createNewFile();
System.out.println("创建文件完成");
}catch(IOException e) {
System.out.println("文件创建失败");
e.printStackTrace();
}
}else {
System.out.println("文件已经存在");
}
return f;
}
}
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class StreamTest {
public static void main(String[] args) {
StreamTest st = new StreamTest();
String writeStr = "Hello World!\r\n你好!";
String fileName = "outFile.txt";
st.OutputTest(fileName,writeStr);
st.InputTest(fileName);
}
//字节-读
private void InputTest(String fileName) {
File f = createFile(fileName);
FileInputStream fis;
byte[] b = new byte[100];
try {
System.out.println("创建输入流...");
fis = new FileInputStream(f);
System.out.println("创建输入流完成");
System.out.println("开始读取...");
fis.read(b);
System.out.println("读取完成");
String str = new String(b);
System.out.println("读取内容输出:\n"+str);
fis.close();
}catch(FileNotFoundException e) {
System.out.println("文件没有找到");
}catch(IOException e) {
System.out.println("读取失败");
}
}
//字节-写
private void OutputTest(String fileName,String text) {
File f = createFile(fileName);
FileOutputStream fos;
try{
System.out.println("创建输出流...");
fos = new FileOutputStream(f);
System.out.println("创建输出流完成");
byte[] testBArray = text.getBytes();
System.out.println("开始写数据...");
fos.write(testBArray);
fos.flush();
System.out.println("写数据完成");
fos.close();
}catch(FileNotFoundException e) {
System.out.println("文件没有找到");
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
//创建文件
private File createFile(String fileName) {
File f = new File(fileName);
if(!f.exists()) {
System.out.println("文件不存在");
try{
System.out.println("创建文件...");
f.createNewFile();
System.out.println("创建文件完成");
}catch(IOException e) {
System.out.println("文件创建失败");
e.printStackTrace();
}
}else {
System.out.println("文件已经存在");
}
return f;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询