创建文件
import java.io.*;
class Demo2 {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("demo.txt");
fw.write("xxxx");
}
catch (IOException e) {
System.out.println(e.toString());
}
finally {
try {
if (fw != null) //关键
fw.close();
}
catch (IOException e) {
System.out.println(e.toString());
}
}
}
}
续写
import java.io.*;
class Demo3 {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("demo.txt",true);
fw.write("hahah");
}
catch (IOException e) {
System.out.println(e.toString());
}
finally {
try {
if(fw != null)
fw.close();
}
catch (IOException e) {
System.out.println(e.toString());
}
}
}
}
读取
import java.io.*;
class DemoReader {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader ("demo.txt");
int ch = 0;
while ((ch = fr.read())!=-1) {
System.out.println((char)ch);
}
}
}
import java.io.*;
class ReaderDemo {
public static void main(String[] args)throws IOException {
FileReader fr =new FileReader("Demo.java");
char[] buf = new char [1024]; //将字符读入数组
int num = 0;
while((num = fr.read(buf))!=-1) {
System.out.print(new String(buf,0,num)); //从0开始 取num个
}
fr.close();
}
}
拷贝
import java.io.*;
class CopyDemo {
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("demo.java");
fw = new FileWriter("democopy.txt");
char[] buf = new char [1024];
int num = 0;
while ((num=fr.read(buf)) !=-1) {
String st = new String (buf,0,num);
fw.write(st);
fw.flush();
}
}
catch(IOException e) {
throw new RuntimeException("失败");
}
finally {
if(fr!=null)
try {
fr.close();
}
catch(IOException e) {
System.out.println(e.toString());
}
if(fw!=null)
try {
fw.close();
}
catch(IOException e) {
System.out.println(e.toString());
}
}
}
}
缓冲区
import java.io.*;
class BufferedWriterDemo {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("xxx.txt");
BufferedWriter bf = new BufferedWriter(fw); // 缓冲区
bf.write("xxxxx");
bf.newLine(); //换行
bf.write("xxxxx");
bf.flush();
bf.close(); //关闭缓冲区就是在关闭缓冲区的流对象 不需要fw.close()
}
}
import java.io.*;
class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader ("xxx.txt");
BufferedReader br = new BufferedReader(fr);
String s = null;
while((s = br.readLine()) != null) { //读一行
System.out.println(s);
}
br.close();
}
}
复制
import java.io.*;
class CopyDemo2 {
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader (new FileReader("RandomDemo.java"));
bw = new BufferedWriter (new FileWriter("Cpzz.txt"));
String s = null;
while ((s = br.readLine()) !=null) {
bw.write(s);
bw.newLine();
bw.flush();
}
}
catch(IOException e) {
throw new RuntimeException("shibai");
}
finally {
try {
if(br != null)
br.close();
}
catch(IOException e) {
throw new RuntimeException("shibai");
}
try {
if(bw != null)
bw.close();
}
catch(IOException e) {
throw new RuntimeException("shibai");
}
}
}
}
import java.io.*;
class Demo {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader ("demo.java");
LineNumberReader lr = new LineNumberReader(fr);
String s = null;
lr.setLineNumber(3); //从3开始
while ((s =lr.readLine()) != null) {
System.out.println(lr.getLineNumber()+ "" +s);
}
lr.close();
}
}
。。
import java.io.*;
class MyLineNumberReader {
private Reader r;
private int lineNum;
MyLineNumberReader (Reader r) {
this.r = r;
}
public String myReadLine () throws IOException { //读一行方法
lineNum++;
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 myClose() throws IOException {
r.close();
}
public void setLineNumber (int lineNum) {
this.lineNum = lineNum;
}
public int getLineNumber () {
return lineNum;
}
}
class MyLineDmeo {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("demo.java");
MyLineNumberReader mr = new MyLineNumberReader(fr);
String s = null;
mr.setLineNumber(1);
while((s = mr.myReadLine()) != null) {
System.out.println(mr.getLineNumber()+":"+s);
}
mr.myClose();
}
}