一. File类
file对应着一个文件或文件夹。
绝对路径:包括盘符在内的完整的文件路径;
相对路径:在当前文件目录下的文件的路径。
File file1 = new File("f:/JAVA/io/Young.txt"); //绝对路径
File file2 = new File("hello1.txt"); //相对路径
System.err.println(file1.getName()); //获取文件名
System.out.println(file1.getPath()); //获取路径
System.out.println(file1.getAbsolutePath()); //获取绝对文件路径
System.out.println(file1.getAbsoluteFile()); //获取绝对文件名(显示绝对路径)
System.out.println(file1.getParent()); //获取上层目录
/*
* 同样可操作文件目录
* */
System.out.println(file1.exists()); //文件是否存在
System.out.println(file1.canWrite()); //文件是否可写
System.out.println(file1.canRead()); //文件是否可读
System.err.println(file1.isFile()); //是否为文件
System.out.println(file1.isDirectory()); //是否为文件目录
System.out.println(file1.lastModified()); //文件最后修改时间(返回long型的值)
System.out.println(file1.length()); //文件内容有多大(返回long型的值)
二. IO流:
1.输入流:读取数据
(1)字节流(最好用于非文本文件):
抽象基类:InputStream,
节点流:FileInputStream,
缓冲流:BufferedInputStream
/*
*用字节流实现非文本文件的复制粘贴
*/
public static void case1{
//3.将创建的节点流的对象作为形参传递给缓冲流的构造器中
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
//1.提供读入写出的文件
File file1 = new File("");
File file2 = new File("");
//2.创建相应的节点流
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//4.具体的实现文件复制的操作
byte[] b = new byte[1024];
int len;
while((len = bis.read(b)) != -1)
{
bos.write(b,0,len);
bos.flush();//刷新
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if(bis != null)
{
try
{
bis.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
if(bos != null)
{
try
{
bos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
(2)字符流(用于文本文件):
抽象基类:Reader,
节点流:FileReader,
缓冲流:BufferedReader
public static void case2()
{
/*
* 使用字符流实现文件复制粘贴
*/
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(""));
bw = new BufferedWriter(new FileWriter(""));
String str;
while((str = br.readLine()) != null)
{
bw.write(str);
bw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(br != null)
{
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bw != null)
{
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2.输出流:写入数据
(1)字节流:
抽象基类:OutputStream,
节点流:FileOutputStream,
缓冲流:BUfferedOutputStream
(2)字符流:
抽象基类:Writer,
节点流:FileWriter,
缓冲流:BufferedWriter
三.转换流:
InputStreamReader,OutputStreamWriter
编码:字符串->字节数组(InputStreamReader)
解码:字节数组->字符串(OutputStreamWriter)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public static void main(String[] args) {
//解码:
BufferedReader br = null;
BufferedWriter bw = null;
try
{
File file = new File("");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis,"GBK");//"GBK"->用GBK码转换
br = new BufferedReader(isr);
//编码:
File file1 = new File("");
FileOutputStream fos = new FileOutputStream(file1);
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
bw = new BufferedWriter(osw);
String str;
while((str = br.readLine()) != null)
{
bw.write(str);
bw.newLine();
bw.flush();
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if(br != null)
{
try
{
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if(bw != null)
{
try
{
bw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
四:标准输入输出流
输入:System.in
输出:System.out
五:数据流(用来处理基本数据类型,String,字节数组的数据)
[DataInputStream][DataOutputStream]
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public static void main(String[] args) {
DataOutputStreamdemo();
DataInputStreamdemo();
}
public static void DataOutputStreamdemo()
{
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream(new File("")));
dos.writeUTF("str");
dos.writeLong(10241024);
dos.writeBoolean(true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(dos != null)
{
try {
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void DataInputStreamdemo()
{
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream(new File("")));
String str = dis.readUTF();
System.out.println(str);
Long l = dis.readLong();
System.out.println(l);
boolean b = dis.readBoolean();
System.out.println(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(dis != null)
{
try {
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}