import java.io.*;
class FileWriterDemo
{
public static void main(String[] args) throws IOException
{
//创建一个FileWriter对象,该对象一被初始化,就必须要明确被操作的文件。
//没有此文件 将创建文件,如有同名文件,将被覆盖。
FileWriter fw = new FileWriter("demo.txt");
//write方法将字符串写入到流中。
fw.write("hello");
//刷新缓冲区中数据,写入目的地中。
//fw.flush();
//关闭流,并刷新一次缓冲中的数据。
fw.close();
}
}
字符流读取方法
以FileReader为例:
常用方法:
read() //读取一个字符,返回字符对应的ACSII码
close() //关闭读取流
读取文件示例:
import java.io.*;
class FileReaderDemo
{
public static void main(String[] args) throws IOException
{
//第一种读取方式:单个字符读取
FileReader fr = new FileReader("demo.txt");
while(true)
{
int c = fr.read();
if(c == -1)
break;
System.out.print((char)c);
}
fr.close();
//第二种方式:通过字符数组进行读取。
//read(char[])返回的是读到的字符个数
FileReader fr2 = new FileReader("demo.txt");
char[] buf = new char[1024];
int num = 0;
while((num = fr2.read(buf)) != -1)
System.out.print(new String(buf, 0, num));
fr2.close();
}
}
import java.io.*;
class BufferedReaderDemo
{
public static void main(String[] args) throws IOException
{
FileReader fr = new FileReader("test.java");
BufferedReader bufr = new BufferedReader(fr);
String s = null;
while((s = bufr.readLine()) != null)
{
System.out.println(s);
}
}
}
class BufferedWriterDemo
{
public static void main(String[] args) throws IOException
{
FileWriter fw = new FileWriter("buf.txt");
BufferedWriter bufw = new BufferedWriter(fw);
bufw.write("abcde");
bufw.flush();
bufw.close();//关闭缓冲区也直接关闭了FileWriter对象;
}
}
import java.io.*;
//自定义BufferedReader类
class MyBufferedReader extends Reader
{
private Reader r;
MyBufferedReader(Reader r)
{
this.r = r;
}
//一次读一行数据的方法
public String myReadLine()throws IOException
{
//定义一个临时容器,原BufferedReader使用的是字符数组,
//这里为了方便使用StringBuilder,
StringBuilder sb = new StringBuilder();
int ch = 0;
while((ch = r.read()) != -1)
{
//判断是否为行终止符
if(ch == '\r')
continue;
if(ch == '\n')
return sb.toString();
else
sb.append((char)ch);
}
if(sb.length() != 0)
{
return sb.toString();
}
return null;
}
public void close()throws IOException
{
r.close();
}
public int read(char[] cbuf, int off, int len)throws IOException
{
return r.read(cbuf, off, len);
}
}
class MyBufferedReaderDemo
{
public static void main(String[] args)throws IOException
{
FileReader fr = new FileReader("test.java");
MyBufferedReader mbufr = new MyBufferedReader(fr);
FileWriter fw = new FileWriter("buf.txt");
BufferedWriter bufw = new BufferedWriter(fw);
String buf = null;
while((buf = mbufr.myReadLine()) != null)
{
fw.write(buf);
bufw.newLine();
bufw.flush();
}
mbufr.close();
fw.close();
}
}
import java.io.*;
class CopyPic
{
public static void picCopy()
{
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
fos = new FileOutputStream("buf.jpg");
fis = new FileInputStream("logo.jpg");
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf)) != -1)
{
fos.write(buf, 0, len);
}
}
catch(IOException e)
{
System.out.println("pic copy failed");
}
finally
{
try
{
if(fos != null)
fos.close();
}
catch(IOException e)
{
System.out.println("output close failed");
}
try
{
if(fos != null)
fos.close();
}
catch(IOException e)
{
System.out.println("input close failed");
}
}
}
public static void main(String[] args)
{
picCopy();
}
}
import java.io.*;
class MyBufferedInputStream
{
private InputStream in;
private byte[] buf = new byte[1024];//缓冲区
private int pos = 0, count = 0;//定义指针,计数器
MyBufferedInputStream(InputStream in)
{
this.in = in;
}
//一次读一个字节,从缓冲区——字节数组中读
public int myRead()throws IOException
{
if(count == 0)//数组是否取空
{
count = in.read(buf);//通过in对象读取硬盘上的数据 并存储到缓冲区buf中,
if(count < 0)
{
return -1;
}
pos = 0;
byte b = buf[pos];//第一次从缓冲区中取一个字节
count --;
pos ++;
return b & 0xff;//补0
}else if(count > 0)
{
byte b = buf[pos];//一次从缓冲区中取一个字节
count --;
pos ++;
return b & 255;//补0
}
return -1;
}
public void close()throws IOException
{
in.close();
}
}
/* byte型提升为int型后,8位数据提升为32位,前面系统自动补1,如果独到8个1,补位后
* 还是-1,就会中断读数据循环,所以要手动补0。就和255与运算。
*
*/
import java.io.*;
class TransStreamDemo
{
public static void main(String[] args)throws IOException
{
//获取键盘录入对象
// InputStream in = System.in;
// //将字节流转换成字符流,使用转换流
// InputStreamReader isr = new InputStreamReader(in);
// BufferedReader br = new BufferedReader(isr);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// OutputStream out = System.out;
// OutputStreamWriter osw = new OutputStreamWriter(out);
// BufferedWriter bw = new BufferedWriter(osw);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String buf = null;
while((buf = br.readLine()) != null)
{
if("over".equals(buf))
break;
bw.write(buf.toUpperCase());
bw.newLine();
bw.flush();
}
br.close();
br.close();
bw.close();
}
}
import java.io.*;
class TransStreamDemo
{
public static void main(String[] args)throws IOException
{
//获取键盘录入对象
// InputStream in = System.in;
// //将字节流转换成字符流,使用转换流
// InputStreamReader isr = new InputStreamReader(in);
// BufferedReader br = new BufferedReader(isr);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// OutputStream out = System.out;
// OutputStreamWriter osw = new OutputStreamWriter(out);
// BufferedWriter bw = new BufferedWriter(osw);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String buf = null;
while((buf = br.readLine()) != null)
{
if("over".equals(buf))
break;
bw.write(buf.toUpperCase());
bw.newLine();
bw.flush();
}
br.close();
br.close();
bw.close();
}
}