文件:就是保存数据的地方,它既可以保存一张图片,也可以保存视频,声音.....
文件在程序中是以流的形式来操作的
流:数据在数据源(文件)和程序(内存)之间经历的路径
输入流:数据从数据源(文件)到程序(内存)的路径
输出流:数据从程序(内存)带数据源(文件)的路径
(以java程序为准,向它输入的就是输入流,从它输出的就是输出流)
创建文件的三种方式
注意:在java编程中,目录也被当为文件,new File()
@Test
public void create01(){
//1.public File(String pathname)
//一个\会报错,因为需要转义\
String filepath="d:\\file-study\\news1.txt";
//在内存(java程序)里创建一个file对象,但没有和硬盘发生任何关系
File file1 = new File(filepath);
try {
//在执行creatNewFile之后,把file信息写入到对应的path路径(磁盘)中,真正的成为文件
file1.createNewFile();
System.out.println("public File(String pathname)文件创建成功!");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Test
public void create02(){
//2.public File(File parent, String child)
//父目录文件+子路径构建
//一个\会报错,因为需要转义\
File parentfile=new File("d:\\file-study");
//File parentfile=new File("d:\\file-study\\");也可以
String filename="news2.txt";
File file2 = new File(parentfile, filename);
try {
file2.createNewFile();
System.out.println("public File(File parent, String child)文件创建成功");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Test
public void create03(){
//3. public File(String parent, String child)
//父目录+子路径
String parentpath="d:\\file-study";
String filename="news3.txt";
File file3 = new File(parentpath, filename);
try {
file3.createNewFile();
System.out.println("public File(String parent, String child)文件创建成功");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
获取文件信息
//获取文件信息
@Test
public void info(){
//先创建文件信息
File file = new File("D:\\file-study\\news1.txt");
//调用相应的方法,得到对应信息
System.out.println("文件名:"+file.getName());
System.out.println("绝对路径:"+file.getAbsolutePath());
System.out.println("文件:"+file.length());
System.out.println("文件是否存在:"+file.exists());
}
文件删除
@Test
public void Dir(){
//判断文件是否存在,如果存在就删除
String filepath="D:\\file-study\\news1.txt";
File file = new File(filepath);
if(file.exists()){
System.out.println(file.delete());
}else {
System.out.println("该文件不存在");
}
}
创建目录
创建一级目录用mkdir()方法
创建多级目录用mkdirs()方法
@Test
public void DIR2(){
//判断目录是否存在,如果存在就提示已存在,否则创建
String filepath="D:\\file-study\\a\\b\\c";
File file = new File(filepath);
if(file.exists()){
System.out.println("该目录已存在");
}else {
file.mkdirs();
}
}
I/O技术
I/O技术是非常实用的技术,用于处理数据传输,如读写文件,网络通讯等。
原理
input:读取外部数据(磁盘,光盘等储存设备的数据)到程序(内存)中
output:将程序(内存)数据输出到磁盘,光盘等存储设备中
流的分类
按操作数据单位分类:字节流(一个字节8bit,二进制文件)和字符流(按字符,文本文件)
按数据流流向:输入流,输出流
按流的角色:节点流,处理流/包装流
注意:字节流为byte[ ],字符流为char[ ]。要熟悉byte和char转化为String的方法
(抽象基类) | 字节流 | 字符流 |
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
InputStream
FileInputStream
使用read()读取文件,一个字节一个字节读
@Test
public void test1(){
//读取路径
String filepath="D:\\file-study\\hello.txt";
int readData=0;
FileInputStream fileInputStream=null;
try {
//创建FileInputStream对象,读取文件
fileInputStream = new FileInputStream(filepath);
//从该输入流读取一个字节的数据.如果没有输入可用,此方法将阻止
//结果:如果数据的下一个字节达到文件的末尾,返回-1
while((readData=fileInputStream.read())!=-1){
//输入汉字会乱码,因为read是一个一个字节读,而一个汉字为3个字节.
System.out.print((char)readData);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
{
//关闭文件流,释放资源
try {
fileInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
使用read(byte[] b)读取文件,提高效率
@Test
public void test2(){
//读取路径
String filepath="D:\\file-study\\hello.txt";
//字节数组
byte[]buf=new byte[8];//一次读取8个字节
//实际读取长度
int readLen=0;
FileInputStream fileInputStream=null;
try {
//创建FileInputStream对象,读取文件
fileInputStream = new FileInputStream(filepath);
//从该输入流读取最多buf.length长度的字节数据到字节数组buf.如果没有输入可用,此方法将阻止
//结果:如果读取正常,返回实际读取的字节数
//如果返回-1,表示读取完毕
while((readLen=fileInputStream.read(buf))!=-1){
//输入汉字会乱码,因为read是一个一个字节读,而一个汉字为3个字节.
System.out.print(new String(buf,0,readLen));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
{
//关闭文件流,释放资源
try {
fileInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
FileOutputStream
wirte()方法的三种用法
/*
* new FileOutputStream(filepath)创建方式.当写入内容时,会覆盖原来内容
* new FileOutputStream(filepath, true)创建方式,当写入内容时,是追加到文件后面
* */
@Test
public void test1(){
//读取路径,若该文件不存在,则创建一个
String filepath="D:\\file-study\\a.txt";
//创建一个FileOutputStream对象
FileOutputStream fileOutputStream=null;
try {
//得到FileOutputStream对象
fileOutputStream = new FileOutputStream(filepath);
//写入一个字节
//public void write(int b) throws IOException
fileOutputStream.write('a');
//写入字符串
//public void write(byte b[]) throws IOException
String str="hello,world";
//str.getBytes()可以把字符串转化为字节数组
//会覆盖前面的内容
fileOutputStream.write(str.getBytes());
//public void write(byte b[], int off, int len) throws IOException
fileOutputStream.write(str.getBytes(),0,str.length());
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
文件拷贝
/*
* 思路分析
* 1.创建文件输入流,将文件读入到程序
* 2.创建文件输出流,将读取的文件数据,写到指定的文件
* */
@Test
public void test1(){
String readpath="D:\\file-study\\study.jpg";
String writepath="D:\\file-study\\study1.jpg";
FileInputStream fileInputStream=null;
FileOutputStream fileOutputStream=null;
try {
fileInputStream=new FileInputStream(readpath);
fileOutputStream=new FileOutputStream(writepath);
byte[] buff = new byte[1024];
int readLen=0;
while((readLen=(fileInputStream.read(buff)))!=-1){
//读取后就写入到文件,通过fileoutStream
//即一边读一边写
fileOutputStream.write(buff,0,readLen);
}
System.out.println("拷贝成功!");
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
//关闭输入流和输出流,释放资源
fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
FileReader
使用read()读取文件,一个字符一个字符读
/*
* 使用read()读取文件,一个字符一个字符读
* */
@Test
public void test1(){
//读取路径
String filepath="D:\\file-study\\story.txt";
int readData=0;
FileReader fileReader=null;
try {
//创建FileReader对象,读取文件
fileReader=new FileReader(filepath);
//从该输入流读取一个字符的数据.如果没有输入可用,此方法将阻止
//结果:如果数据的下一个字符达到文件的末尾,返回-1
while((readData=fileReader.read())!=-1){
System.out.print((char) readData);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
{
//关闭文件流,释放资源
try {
fileReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
使用字符数组读取文件,提高效率
/*
* 使用字符数组读取文件,提高效率
* */
@Test
public void test2(){
//读取路径
String filepath="D:\\file-study\\story.txt";
//字符数组
char[]buf=new char[8];//一次读取8个字符
//实际读取长度
int readLen=0;
FileReader fileReader=null;
try {
//创建fileReader对象,读取文件
fileReader=new FileReader(filepath);
//从该输入流读取最多buf.length长度的字符数据到字符数组buf.如果没有输入可用,此方法将阻止
//结果:如果读取正常,返回实际读取的字符数
//如果返回-1,表示读取完毕
while((readLen=fileReader.read(buf))!=-1){
System.out.print(new String(buf,0,readLen));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
{
//关闭文件流,释放资源
try {
fileReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
FileWriter
注意:在FilterWriter之后,必须要关闭(close)或者刷新(flush),否则所写的数据可能还在内存,写入不到指定的文件。
wirte()方法的三种用法
/*
* 使用字符数组读取文件,提高效率
* */
@Test
public void test2(){
//读取路径
String filepath="D:\\file-study\\story.txt";
//字符数组
char[]buf=new char[8];//一次读取8个字符
//实际读取长度
int readLen=0;
FileReader fileReader=null;
try {
//创建fileReader对象,读取文件
fileReader=new FileReader(filepath);
//从该输入流读取最多buf.length长度的字符数据到字符数组buf.如果没有输入可用,此方法将阻止
//结果:如果读取正常,返回实际读取的字符数
//如果返回-1,表示读取完毕
while((readLen=fileReader.read(buf))!=-1){
System.out.print(new String(buf,0,readLen));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
{
//关闭文件流,释放资源
try {
fileReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}