输入输出
一、file类
1.常用方法:
file.exists() 判断文件是否存在
file.isFile() 判断是否是文件
file.isDirectory() 判断是否是目录
file.getPath() 得到相对路径
file.getAbsolutePath() 得到绝对路径
file.getName() 得到文件名称
file.delete() 删除指定文件
file.createNewFile() 创建新文件
二、输入输出流
InputStream Reader 输入
package file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TestFile {
public static void main(String[] args) {
File file = new File("d:/a.txt");
FileInputStream f = null;
try {
f = new FileInputStream(file);
int a;
while((a=f.read())!=-1){
System.out.print((char)a);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
f.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
OutputStream Writer 输出
package file;
import java.io.*;
public class OutputTest {
public static void main(String[] args) {
File file = new File("D:/b.txt");
FileOutputStream fo = null;
try {
fo = new FileOutputStream(file);
byte[] b = new byte[10];
b[0] = 'a';
b[1] = 'b';
fo.write(b);
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Reader 读取
package file;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.text.AbstractDocument.BranchElement;
public class ReaderTest {
public static void main(String[] args) throws Exception {
FileReader fr = null;
BufferedReader bf = null;
try {
fr = new FileReader("D:/a.txt");//要读取的文件路径
bf = new BufferedReader(fr);//讲文件中的内容放在缓冲区中
String line;//定义一个变量,每次读取一行,存储在这个变量中
while((line=bf.readLine())!=null){//每次读取一行,知道读取没有内容
System.out.println(line);//打印读取的一行数据
}
} catch (Exception e) {
e.printStackTrace();
}finally{//关闭资源
bf.close();
fr.close();
}
}
}
writer
package file;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WriterTest {
public static void main(String[] args) {
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter("D:/c.txt");
bw = new BufferedWriter(fw);
bw.write("我是小黑,但是我就是很黑");
bw.newLine();//换行
bw.write("我是小白,但是我就是不白");
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
一、file类
1.常用方法:
file.exists() 判断文件是否存在
file.isFile() 判断是否是文件
file.isDirectory() 判断是否是目录
file.getPath() 得到相对路径
file.getAbsolutePath() 得到绝对路径
file.getName() 得到文件名称
file.delete() 删除指定文件
file.createNewFile() 创建新文件
二、输入输出流
InputStream Reader 输入
package file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TestFile {
public static void main(String[] args) {
File file = new File("d:/a.txt");
FileInputStream f = null;
try {
f = new FileInputStream(file);
int a;
while((a=f.read())!=-1){
System.out.print((char)a);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
f.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
OutputStream Writer 输出
package file;
import java.io.*;
public class OutputTest {
public static void main(String[] args) {
File file = new File("D:/b.txt");
FileOutputStream fo = null;
try {
fo = new FileOutputStream(file);
byte[] b = new byte[10];
b[0] = 'a';
b[1] = 'b';
fo.write(b);
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Reader 读取
package file;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.text.AbstractDocument.BranchElement;
public class ReaderTest {
public static void main(String[] args) throws Exception {
FileReader fr = null;
BufferedReader bf = null;
try {
fr = new FileReader("D:/a.txt");//要读取的文件路径
bf = new BufferedReader(fr);//讲文件中的内容放在缓冲区中
String line;//定义一个变量,每次读取一行,存储在这个变量中
while((line=bf.readLine())!=null){//每次读取一行,知道读取没有内容
System.out.println(line);//打印读取的一行数据
}
} catch (Exception e) {
e.printStackTrace();
}finally{//关闭资源
bf.close();
fr.close();
}
}
}
writer
package file;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WriterTest {
public static void main(String[] args) {
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter("D:/c.txt");
bw = new BufferedWriter(fw);
bw.write("我是小黑,但是我就是很黑");
bw.newLine();//换行
bw.write("我是小白,但是我就是不白");
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}