package com.great.view;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
/*
* IO流
* IO:in/out这两个英文首字母
* 流:从一端到另一端单向的传递方式
*
* --- 数据进出的流动方式
*
* 1.非流式IO --- 以File这个类为代表,还有别的非流式IO
* 2.流式IO
*
* 文件操作方法:
* boolean canRead() 是否可读
boolean canWrite() 是否可修改(写)
boolean setReadOnly() 设置文件内部数据不能修改
boolean exists() 判断文件是否存在
boolean isDirectory() 判断该文件是否是文件夹
boolean isFile() 判断该文件是否是文件
boolean isHidden() 判断该文件是否隐藏
long lastModified() 得到最后修改时间
boolean setLastModified(long time) 设置最后修改时间
long length() 文件内容的长度按byte(字节)来计算
*
*
* String getName() 得到文件名
File getParentFile() 上一级的文件对象
String getParent() 上一级的文件名
String getPath() 得到文件路径
boolean isAbsolute() 是否是绝对路径
File getAbsoluteFile() 得到绝对路径的文件对象
String getAbsolutePath() 得到绝对路径的路径名
File getCanonicalFile() 得到相对路径
String getCanonicalPath() 得到相对路径的路径名
*
*
* 流式IO:
* 特点:
* 1.有起点有终点
* 2.单向传输
* 3.起点和终点可以交换
*
* 数据分为 读 和 写 , in 、out 关系?
* 读 --- in
* 写 --- out
*
* 注意:
* 如果类名中出现Stream就是字节流
* 如果出现in 就是读入
* 如果出现out 就是写出
* 如果出现reader 就是读入字符
* 如果出现writer 就是写出字符
*
*
*
*/
public class Demo {
public static void main(String[] args) {
/* File f = new File("files/aaa.txt");//初始化文件的方法一
File f1 = new File("E://test", "login.txt");//初始化文件的方法二
File f2 = new File(new File("E://test"), "login.txt");//初始化文件的方法三
System.out.println(new File("E://test"));*/
File f2 = new File("E://test/login.txt"); //初始化文件
f2.setReadOnly();
System.out.println(f2.exists()); //判断文件是否存在,存在返回true
System.out.println(f2.isDirectory()); //判断是否是文件夹
long t = f2.lastModified(); //得到文件的最后修改时间
System.out.println(t);
Date d = new Date(t);
System.out.println(d);
//Calendar:万年历
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(t);
System.out.println(calendar.getTime());
File f3 = new File("E://test/log.txt");
//注意:中文占2个字节,英文1个字节
System.out.println(f3.length()); //返回文件内容的长度按byte(字节)来计算
File f4 = new File("E://");
String[] str1 = f4.list();
for(String s:str1){
System.out.println(s);
}
System.out.println("======================================================");
//FilenameFilter 文件名过滤器
String[] str2 = f4.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if(name.endsWith(".txt")){ //如果文件名的最后的字符是 .txt就返回true
return true;
}
return false;
}
});
for(String s:str2){
System.out.println(s);
}
/* f4.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
// TODO Auto-generated method stub
return false;
}
});*/
File f5 = new File("E://t/a/b/c/d/e");
f5.mkdirs(); //可以创建子文件夹
File f6 = new File("E://regest.txt");
if(f6.exists() == false){
try {
f6.createNewFile(); //创建文件
} catch (IOException e) {
e.printStackTrace();
}
}
f6.delete();
File f7 = new File("E://test.txt");
File f8 = new File("F://刘彬.txt");
f7.renameTo(f8); //重命名+剪切
//创建临时文件,后缀是.tmp格式的
File f9 = new File("E://");
try {
File f10 = File.createTempFile(System.currentTimeMillis()+"", ".tmp", f9);
System.out.println(f10);
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.great.view;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileDemo1 {
/*
* 读取文件信息:
* 1.创建文件对象
* 2.创建字节读入流对象
*
*
*
*/
public static void main(String[] args) {
try {
File f = new File("E://log.txt");
//读取文件信息
FileInputStream fips = new FileInputStream(f);
//int i = fips.read(); //读取一个字节,返回的是该字节的ascII码,如果读到末尾了,返回-1
//System.out.println((char)i);
/* int i = 0;
i = fips.read();
while( i != -1 ){
System.out.print((char)i);
i = fips.read();
}*/
/* int j = 0;
while((j = fips.read())!=-1){
System.out.print((char)j);
}*/
//一个个字节读取,效率低,并且出现中文乱码的现象
//实现批量读取这样就会缓解上面效率低下,乱码的问题
/* byte[] b = new byte[1024];
int len = 0;
while((len = fips.read(b))!=-1){
System.out.print(new String(b, 0, len));//参数 字节数组b 第0位开始 读取len 长度
}*/
//创建写出文件对象
File f1 = new File("F://log.txt");
FileOutputStream fops = new FileOutputStream(f1); //文件如果不存在会自动创建
byte[] b = new byte[1024];
int len = 0;
while((len = fips.read(b))!=-1){
//System.out.print(new String(b, 0, len));
fops.write(b, 0, len);
}
fops.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.great.view;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
//字符读取,写入文件
public class FileDemo2 {
public static void main(String[] args) {
try {
FileInputStream fips = new FileInputStream("E://log.txt");//读取字节文件信息
InputStreamReader ipsr = new InputStreamReader(fips); //字节转换成字符
BufferedReader reader = new BufferedReader(ipsr); //读取字符文件信息
/* String s = reader.readLine(); //所有内容只读出第一行的内容
System.out.println(s);*/
/*String s = "";
String msg = "";
while((s = reader.readLine()) != null){
msg += s;
}
System.out.println(msg);*/ //把所有内容放在一行里面读出
//------------------------------------------------------------------------------
PrintWriter pw = new PrintWriter("F://aaa.txt");
String s = "";
while((s = reader.readLine()) != null){
pw.println(s);
}
pw.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}