一、文件
1、基本介绍
java使用File类来表示计算机系统磁盘文件的对象类型。File类的一个对象,代表一个文件或一个文件目录(即文件夹)。
2、常用的文件操作
(1)常用方法与构造器
方法:createNewFile创建新文件
构造器:
new File(String pathname)//根据路径构建一个File对象
// new File(String pathname)//根据路径构建一个File对象
//在d盘下创建news1.txt
String filepath="d:\\news1.txt";
File file=new File(filepath);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
new File(File parent,String child)//根据父目录文件+子路径构建
//new File(File parent,String child)//根据父目录文件+子路径构建
//在d盘下创建news2.txt
File parentFile=new File("d:\\");
String fileName= "news2.txt";
File file=new File(parentFile,fileName);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
new File(String parent,String child)//根据父 目录+子路径构建
//new File(String parent,String child)//根据父目录+子路径构建
//在d盘下创建news3.txt
String parentFile= "d:\\";
String fileName= "news3.txt";
File file=new File(parentFile,fileName);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
(2)获取文件信息
public class FileInformation {
@Test
public static void main(String[] args) {
//getName,getAbsolutePath,getParent,length,exists,isFile,isDirectory
//先创建文件
File file=new File("d:\\news1.txt");
//调用相应方法,得到对应信息
System.out.println("文件名字="+file.getName());
System.out.println("文件绝对路径="+file.getAbsolutePath());
System.out.println("文件父级目录="+file.getParent());
System.out.println("文件大小(字节)="+file.length());
System.out.println("是不是一个文件="+file.isFile());
System.out.println("是不是一个目录="+file.isDirectory());
}
}
二·、Java IO流原理
1、I/O是Input/Output的缩写,I/O技术是非常实用的技术,用于处理数据传输。
如读/写文件,网络通讯等
2、java程序中,对于数据的输入/输出操作以“流(stream)”的方式进行。
3、java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过方法输入或输出数据。
4、输入input:读取外部数据(磁盘、光盘等储存设备的数据)到程序(内存)中。
5、输出output:将程序(内存)数据输出到磁盘、光盘等储存设备中。
三、IO流的分类
1、按操作数据单位不同分为:字节流(8 bit),字符流
2、按数据流的流向不同分为:输入流、输出流
3、按流的功能可以分为底层的节点流和上层的处理流。
(javaIO流共涉及40多个类,都是从如下4个抽象基类派生的。InputStream和Reader是所有输入流的基类;OutputStream和Writer是所有输出流的基类。InputStream和OutputStream是字节流的抽象父类;Reader和Writer是字符流的抽象父类)
(1)InputStream类
InputStream类用来表示那些从不同数据源产生输入的类,这些数据源包括字节数组
(ByteArrayInputStream)、字符串(StringBufferInputStream)、文件(FileInputStream)
等等
常用方法:
int read()
从输入流读取数据的下一个字节。
int read(byte[] b)
从输入流读取一些字节数,并将它们存储到缓冲区 b
。
int read(byte[] b, int off, int len)
从输入流读取最多 len
字节的数据到一个字节数组。
public void close()throws IOException,
关闭此输入流并释放与流相关联的任何系统资源。
以FileInPutStream为例:
构造方法:
FileInPutStream(File file):通过打开与实际文件的连接创建一个FileInPutStream,该文件由文件系统中的File对象file 命名。
FileInPutStream(FileDescriptor fdObj):创建FileInPutStream通过使用文件描述符fdObj,其表示在文件系统中的现有连接到一个实际的文件。
FileInPutStream(String name):通过打开与实际文件的连接创建一个FileInPutStream,该文件由文件系统中的路径名name命名。
读取example.txt中的内容
代码如下:
public class A {
public static void main(String[] args) {
}
@Test
public void readFile01(){
String filePath="d:\\example01.txt";
int readData=0;
FileInputStream fileInputStream=null;
try {
fileInputStream=new FileInputStream(filePath);
while ((readData= fileInputStream.read())!=-1){
System.out.print((char) readData);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
运行结果:
(2)OutputStream类
表示输出要去往的目标,如ByteArrayOutputStream(字节数组), FileOutputStream(文件)
常用方法:
public void close()throws IOException,
关闭此输出流并释放与此流相关联的任何系统资源。
public void flush()throws IOException,
刷新此输出流并强制任何缓冲的输出字节被写出
void write(byte[] b)
将 b.length
字节从指定的字节数组写入此输出流。
void write(byte[] b, int off, int len)
从指定的字节数组写入 len
个字节,从偏移 off
开始输出到此输出流。
void write(int b)
将指定的字节写入此输出流。
FileOutputStream类为例:
得到 FileOutputStream对象
说明:
new FileOutputStream(filePath);创建方式,当写入的内容是会覆盖原来的内容时
new FileOutputStream(filePath,true);创建方式,当写入的内容是追加到文件的末尾
使用FileOutputStream在a.txt文件中写入“hello,world”,如果文件不存在,会自动创建文件
public class B {
@Test
public static void main(String[] args) {
//创建FileOutputStream对象
String filePath = "d:\\a.txt";
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(filePath);
String str = "hello,world!";
// fileOutputStream.write(str.getBytes());
fileOutputStream.write(str.getBytes(), 0, str.length());
//write(byte[]b,int off,int len),从off对应的位置开始写len个元素
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
运行结果:
(3)Reader类
public void close()throws IOException,
关闭此输出流并释放与此流相关联的任何系统资源。
int read()
读一个字符
int read(char[] cbuf)
将字符读入数组。
int read(char[] cbuf, int off, int len)
将字符读入数组的一部分。
以FileReader为例:
构造方法:
FileReader(File file):在给定从中读取数据的File的情况下创建一个新FileReader。
FileReader(FileDescriptor fd):在给定从中读取数据的FileDescriptor的情况下创建一个新FileReader。
FileReader(String fileName):在给定从中读取数据的文件名的情况下创建一个新FileReader。
使用
public class FileReaderTest {
public static void main(String[] args) {
String filePath="d:\\example.txt";
FileReader fileReader=null;
int data=0;
try {
fileReader=new FileReader(filePath);
//循环读取,使用read
while ((data=fileReader.read())!=-1){
System.out.print((char)data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileReader!=null){
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
(4)Writer类
常用的方法:
public void close()throws IOException,
关闭此输出流并释放与此流相关联的任何系统资源。
public void flush()throws IOException,刷新流。
void write(char[] cbuf)
写入一个字符数组。
void write(char[] cbuf, int off, int len)
写入字符数组的一部分。
void write(int c)
写一个字符
void write(String str)
写一个字符串
void write(String str, int off, int len)
写一个字符串的一部分。
以FileWriter为例:
FileWriter(File file):当写入的内容是会覆盖原来的内容时
FileWriter(appendFile file boolean );当写入的内容是追加到文件的末尾
public class FileWriterTest {
public static void main(String[] args) {
}
@Test
public void method(){
String filePath="d:\\example03.txt";
FileWriter fileWriter=null;
try {
fileWriter=new FileWriter(filePath);
fileWriter.write("I am fine,thanks. 我很好,谢谢");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("程序结束");
}
}