为了更加灵活的使用IO
所以在IO之前要了解File类 对文件文件夹灵活操作IO才有大用:
类 :File
构造
File("aaa.txt");
构造内可以存放绝对路径和相对路径;路径必须用字符串的方式存放也可以是引用数据
String str = "C:\\aa\\bb";
File(str);
方法();
A:创建功能
1: boolean creatNewFile();创建文件 如果存在这样的文件就不创建了。
2: boolean mkdir(): 创建文件夹 如果存在这样的文件就不创建了。
3: boolean mkdirs(): 创建文件夹 如果父文件不存在。会帮你创建出来;
#3:下面是使用 mkdirs();创建的多级目录;
File dir3 = new File("ccc\\ddd");
boolean b3 = dir3.mkdirs(); //创建多级目录
System.out.println(b3);
B:删除功能
boolean delete(); //删除文件夹,必须删的是空的
C:重命名
boolean renameTo(File f)//如果路径相同就是改名不同就是重新命名;
D:判断功能
boolean isDirectory() //判断是否是目录
boolean isFile(); //判断是否是文件
boolean exists(); //判断是否存在;
boolean canRead(); //判断是否可读;
boolean isHidden(); //判断是否隐藏;
E:获取功能
String getAbsolutePath();//获取绝对路径;
String getPath(); //获取相对路径;
String getName(); //获取名称;
long length(); //获取长度;
long lastModified //获取最后一次的修改时间;
String[] list(); //获取指定目录下所有文件
File[] listFiles():获取指定目录下的所有文件或者文件夹的File数组
-----------------------------------------------------------------------
简单的文件判断:
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个文件路径");
while(true) {
String line = sc.nextLine(); //将键盘录入的文件路径结果存储在line中
File file = new File(line); //封装file对象
if(!file.exists()) {
System.out.println("您录入的文件路径不存在,请重新输入一个文件路径");
}else if(file.isDirectory()) {
System.out.println("您输入的是一个文件夹路径,请重新输入一个文件路径");
}else {
return file;
}
}
----------------------------------------------------------------------------
简单的递归调用:
public static void main(String[] args) {
递归调用(文件夹());
}
private static File 文件夹() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入目录\n格式为: D:xxx\\xxx\\xxx");
String str = sc.nextLine();
File f = new File(str);
if(!(f.exists())){
System.out.println("文件路径不存在");
}else if(f.isFile()){
System.out.println("您输入的是文件");
}
return f;
}
private static void 递归调用(File file){
File[] file1 = file.listFiles();
for (File file2 : file1) {
if(file2.isDirectory()){
递归调用(file2);
}else{
System.out.println(file2.getAbsolutePath());
}
}
}
--------------------------------------------------------------------------
下面进入IO:
--------------------------------------------------------------------------
类 FileInputStream
方法:
int read();//读
int read( byte arr);
void close();//关流
类 FileOutputStream
方法
write(int b);
write(arr,0,len);
void close();//关流
void flush();//刷新
--------------------------------------------------------------------------
第一种
FileInputStream fis = new FileInputStream("C:xxx\\xxx");
FIleOutputstream fos = new FileOutputStream("xxx\\xxx");
int b;
while((int b = fis.read()) != -1){
fos.write();
}
fis.close();
fos.close();
-------------------------------------------------------------------------
第二种
FileInputStream fis = new FileInputStream("C:xxx\\xxx");
FileOutputStream fos = new FileOutputStream("xxx\\xxx");
int len;
byte[] arr = new byte[fis.available()];//创建一个大数组文件多大数组多大。
while(( len = fis.read(arr)) != -1){
fos.write(arr,0,len);
}
fis.close();
fos.close();
-------------------------------------------------------------------
第三种
FileInputStream fis = new FileInputStream("C:xxx\\xxx");
FileOutputStream fos = new FileOutputStream("xxx\\xxx");
int len;
byte[] arr = new byte[8192];
while(( len = fis.read(arr)) != -1){
fos.write(arr,0,len);
}
fis.close();
fos.close();
---------------------------------------------------------------------
第四种
FileInputStream fis = new FileInputStream("xxxxxx"); //创建文件输入流对象,关联文件夹或是音乐等
BufferedInputStream bis = new BufferedInputStream(fis); //创建缓冲区对fis装饰
FileOutputStream fos = new FileOutputStream("xxxxxx"); //创建输出流对象,关联copy.mp3
BufferedOutputStream bos = new BufferedOutputStream(fos); //创建缓冲区对fos装饰
int b;
while((b = bis.read()) != -1) {
bos.write(b);
}
bis.close(); //只关装饰后的对象即可
bos.close();
-----------------------------------------------------------------------------
以上四种建议使用后两种:应为前两种第一个速度慢 第二个数组过大对计算机是不利的:
字节流只读中文是有弊端的有可能读到半个中文:
解决方法有2
* 1,用字符流读(编码表+字节流)
* 2,将文件上的所有字节一次读到内存中,在内存中将所有字节转换成对应的字符串
* ByteArrayOutputStream
-------------------------------------------------------------------------------
public static void demo2() throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("bbb.txt");
fos.write("天下无敌".getBytes());//创建字节流:
fos.write("\r\n".getBytes());
fos.write("额额额".getBytes());
fos.close();
}
public static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("bbb.txt");
byte[] arr = new byte[3]; //创建字节数组
int len;
while((len = fis.read(arr)) != -1) { //先将字节读到数组中
System.out.println(new String(arr,0,len));//转换成字符串打印在控制台,转换的时候有可能乱码
}
fis.close();
}
------------------------------------------------------------------------
下面是标准的异常处理代码
* 在try()中创建的流对象必须实现了AutoCloseable这个接口,如果实现了,在try后面的{}(读写代码)执行后就会自动调用
* 流对象的close方法将流关掉
---------------------------------------------------------------------------------------------
1.7版本之前的IO流标准准处理代码:
public static void main(String[] args) throws IOException {
FileInputStream file = null;
FileOutputStream file2 = null;
try{
file = new FileInputStream("bbb.txt");
file2 = new FileOutputStream("kkk.txt");
int b;
while((b = file.read()) != -1){
file2.write(b);
}
} finally {
try{
if(file !=null);
file.close();
}finally{
if(file2 !=null)
file2.close();
}
}
}
----------------------------------------------------------------------------------------
1.7版本之后的代码:
class xxx{
import java.io.IOException;
public static void main(String[] args) throws IOException {
try(
FileInputStream fis = new FileInputStream("aaa.txt");
FileOutputStream fos = new FileOutputStream("bbb.txt");
MyClose mc = new MyClose();
){
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
}
}
}
class MyClose implements AutoCloseable {
public void close() {
System.out.println("我关了");
}
}
-------------------------------------------------------------------------------yiyi
以上是IO第一天视频总结:loveliness:
|
-
我.png
(163.83 KB, 下载次数: 3)
|