import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class IoStream_Test {
public static void main(String[] args) throws IOException
{
FileIo1();
FileIo2();
FileIo3();
//FileIo4();
FileIo5();
FileIo6();
}
static void FileIo1() throws IOException
{
File f1 = new File("E:\\java4.3.2.1.7workspace\\a.txt");
//流输出目标是文件,会自动创建,如果存在则覆盖
FileOutputStream fos = new FileOutputStream(f1);
byte[] data = "abcde".getBytes();
fos.write(data);
fos.close();
}
static void FileIo2() throws IOException
{
File f1 = new File("E:\\java4.3.2.1.7workspace\\a.txt");
//创建流,true 指需写文件
FileOutputStream fos = new FileOutputStream(f1,true);
//添加换行\r\n
String str = "\r\n"+"xxxx";
fos.write(str.getBytes());
fos.close();
}
static void FileIo3()
{
//发生IO异常时 做的处理
File f = new File("E:\\java4.3.2.1.7workspace\\a.txt");
FileOutputStream fos = null;
try{
fos = new FileOutputStream(f,true);
String str = "\r\n"+"aaaa";
fos.write(str.getBytes());
}
catch(IOException e)
{
System.out.println(e.toString()+"------");
}
finally{
//先判断fos是否是null 只有不为null 才能关闭资源;
if(fos != null)
{
try{
fos.close();
}catch(IOException e)
{
throw new RuntimeException("");
}
}
}
}
{
File f1 = new File("E:\\java4.3.2.1.7workspace\\a.txt");
//创建流,true 指需写文件
FileOutputStream fos = new FileOutputStream(f1,true);
//添加换行\r\n
String str = "\r\n"+"xxxx";
fos.write(str.getBytes());
fos.close();
}
static void FileIo3()
{
//发生IO异常时 做的处理
File f = new File("E:\\java4.3.2.1.7workspace\\a.txt");
FileOutputStream fos = null;
try{
fos = new FileOutputStream(f,true);
String str = "\r\n"+"aaaa";
fos.write(str.getBytes());
}
catch(IOException e)
{
System.out.println(e.toString()+"------");
}
finally{
//先判断fos是否是null 只有不为null 才能关闭资源;
if(fos != null)
{
try{
fos.close();
}catch(IOException e)
{
throw new RuntimeException("");
}
}
}
}
static void FileIo4() throws IOException
{
File file = new File("E:\\java4.3.2.1.7workspace\\a.txt");
//创建输节流
FileInputStream fis = new FileInputStream(file);
//每次独处一个字节
int ch = 0;
while((ch = fis.read())!=-1)
{
System.out.println("ch="+(char)ch);
}
}
{
File file = new File("E:\\java4.3.2.1.7workspace\\a.txt");
//创建输节流
FileInputStream fis = new FileInputStream(file);
//每次独处一个字节
int ch = 0;
while((ch = fis.read())!=-1)
{
System.out.println("ch="+(char)ch);
}
}
static void FileIo5() throws IOException
{
File file = new File("E:\\java4.3.2.1.7workspace\\a.txt");
FileInputStream fis = new FileInputStream(file);
// 另一种读取方法
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf)) != -1)
{
System.out.println(new String(buf,0,len));
}
}
{
File file = new File("E:\\java4.3.2.1.7workspace\\a.txt");
FileInputStream fis = new FileInputStream(file);
// 另一种读取方法
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf)) != -1)
{
System.out.println(new String(buf,0,len));
}
}
static void FileIo6() throws IOException
{
File src = new File("E:\\java4.3.2.1.7workspace\\a.txt");
File dest = new File("E:\\java4.3.2.1.7workspace\\b.txt");
FileOutputStream fos = new FileOutputStream(dest);
FileInputStream fis = new FileInputStream(src);
int len = 0;
byte[] buf = new byte[1024];
while((len = fis.read(buf)) != -1)
{
fos.write(buf,0,len);
}
fis.close();
fos.close();
}
}
{
File src = new File("E:\\java4.3.2.1.7workspace\\a.txt");
File dest = new File("E:\\java4.3.2.1.7workspace\\b.txt");
FileOutputStream fos = new FileOutputStream(dest);
FileInputStream fis = new FileInputStream(src);
int len = 0;
byte[] buf = new byte[1024];
while((len = fis.read(buf)) != -1)
{
fos.write(buf,0,len);
}
fis.close();
fos.close();
}
}