File类的总结:

1.文件和文件夹的创建

2.文件的读取

3.文件的写入

4.文件的复制(字符流、字节流、处理流)

5.以图片地址下载图片

文件和文件夹

相关函数

 (boolean) mkdir() 创建此抽象路径名指定的目录

 (boolean) mkdirs() 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。


 (boolean) delete() 删除此抽象路径名表示的文件或目录


 (boolean) createNewFile() 当不存在此路径名指定名称的文件时,创建一个新的空文件。


 

创建文件

	public static void NewFile(String pathString) {
File file = new File(pathString);
if (!file.exists()) {
try {
if (file.createNewFile()) {
System.out.println("文件创建成功");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
} else {
System.out.println("文件已存在");
}
}

创建文件夹

	public static void NewFileBox(String pathString) {
File file2 = new File(pathString);
if (!file2.exists()) {
if (file2.mkdirs()) {
System.out.println("文件夹成功");
}
} else {
System.out.println("文件夹存在");
file2.delete();//销毁文件
}
}

应用:

	public static void main(String[] args) {
NewFile("test/file.txt");
NewFileBox("test/a/a/a/a");
}

Writer写入文件

用FileWriter写入文件

	public  static void ForFileWriter(String string,String fileName) {
File file = new File(fileName);
try {
FileWriter fWriter = new FileWriter(file);
fWriter.write(string);
fWriter.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

用BufferedWriter写入文件

	public static void ForBufferedWriter(String string,String desFile) {
BufferedWriter bWriter = null;
try {
bWriter = new BufferedWriter(new FileWriter(new File(desFile)));
bWriter.write(string.toString());
bWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}

应用:

	public static void main(String[] args) {
ForFileWriter("用FileWriter写入文件", "test/writer1.txt");
ForBufferedWriter("用BufferedWriter写入文件", "test/writer2.txt");
}

Reader读取文件

用FileReader读取文件

	public static void testReadByReader(String fileName){
File file = new File(fileName);
FileReader fis = null;
try {
fis = new FileReader(file);
char[] arr = new char[1024 * 1000 * 6];
int len = fis.read(arr);
String data = new String(arr, 0, len);
fis.close();
System.out.println(fileName+"中按FileReader读取的文件内容是:\n"+data);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

用FileInputStream读取文件

	public static void testReadByInputStream(String fileName){
File file = new File(fileName);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
byte[] arr = new byte[1024 * 1000 * 6];
int len = fis.read(arr);
String data = new String(arr, 0, len);
fis.close();
System.out.println(fileName+"中按FileInputStream读取的文件内容是:\n"+data);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

用BufferedReader读取文件

	public static void testReadByBufferedReader(String fileName) {
BufferedReader bReader = null;
String line = null;
StringBuffer buffer = new StringBuffer();
try {
bReader =new BufferedReader(new FileReader(new File(fileName)));
while ((line = bReader.readLine())!=null) {
buffer.append(line).append("\n");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println(fileName+"中按BufferedReader读取的文件内容是:\n"+buffer.toString());
}

应用:

	public static void main(String[] args) {
testReadByInputStream("res/我.txt");
testReadByReader("res/我.txt");
testReadByBufferedReader("res/我.txt");
}

文件的复制操作

字符流复制

	public static void FileCopy1(String readfile,String writeFile) {
try {
FileReader input = new FileReader(readfile);
FileWriter output = new FileWriter(writeFile);
int read = input.read();
while ( read != -1 ) {
output.write(read);
read = input.read();
}
input.close();
output.close();
} catch (IOException e) {
System.out.println(e);
}
}

字节流复制

	public static void FileCopy2(String readfile,String writeFile) {
try {
FileInputStream input = new FileInputStream(readfile);
FileOutputStream output = new FileOutputStream(writeFile);
int read = input.read();
while ( read != -1 ) {
output.write(read);
read = input.read();
}
input.close();
output.close();
} catch (IOException e) {
System.out.println(e);
}
}

处理流复制

	public static void FileCopy3(String readfile,String writeFile) {
BufferedReader bReader = null;
BufferedWriter bWriter = null;
String line = null;
try {
bReader = new BufferedReader(new FileReader(new File(readfile)));
bWriter = new BufferedWriter(new FileWriter(new File(writeFile)));
while ((line = bReader.readLine())!=null) {
bWriter.write(line);
bWriter.newLine();
}
bWriter.close();
bReader.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

应用:

	public static void main(String[] args) {
FileCopy1("res/我.txt", "test/1.txt");
FileCopy2("res/我.txt", "test/2.txt");
FileCopy3("res/我.txt", "test/3.txt");
FileCopy2("res/me.jpg", "test/33.jpg");
}

源码下载:

Java学习笔记——File类文件管理及IO读写、复制操作的更多相关文章

  1. Java学习笔记——File类之文件管理和读写操作、下载图片

    Java学习笔记——File类之文件管理和读写操作.下载图片 File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图 ...

  2. Java学习笔记-File类的基本方法

    要渐渐养成写博客的习惯-----> 前段时间看Mars的java中的I/O流没怎么懂,发现I/O流好难啊.今天重新看一遍其他教学,还有书籍,做些笔记,记录下每天的学习生活. File类的一些方法 ...

  3. java学习一目了然——File类文件处理

    java学习一目了然--File类文件处理 File类(java.io.File) 构造函数: File(String path) File(String parent,String child) F ...

  4. Java学习:File类

    Java学习:File类 File类的概述 重点:记住这三个单词 绝对路径和相对路径 File类的构造方法 File类判断功能的方法 File类创建删除功能的方法 File类获取(文件夹)目录和文件夹 ...

  5. Java学习笔记之---类和对象

    Java学习笔记之---类和对象 (一)类 类是一个模板,它描述一类对象的行为和状态  例如:动物类是一个类,动物们都有属性:颜色,动物们都有行为:吃饭 public class Dog { Stri ...

  6. Java学习之File类理解

    File类是io包中唯一代表磁盘文件本身的对象.File类定义了一些与平台无关的方法来操作文件,可以通过调用File类中的方法,实现创建.删除.重命名文件等.File类的对象主要用来获取文件本身的一些 ...

  7. Java学习:File类中的过滤器接口

    javaIO类的File类应用:过滤器接口 FilenameFilter和FileFilter都是用来过滤文件的 例如: 过滤以.jpg或者.java结尾的文件. 通过看他们的源码: 通过使用File ...

  8. java学习笔记--常用类

    一.Math类:针对数学运算进行操作的类 1.常用的方法 A:绝对值   public static int abs(int a) B:向上取整  public static double ceil( ...

  9. Java学习笔记——SequenceInputStream类合并文件的综合举例分析

    SequenceInputStream 介绍 SequenceInputStream 类表示其他输入流的逻辑串联,即文件的合并. 它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾, ...

随机推荐

  1. Java参数传值?or传引用?

    O'Reilly's Javain a Nutshell by David Flanagan (see Resources) puts it best: "javamanipulates o ...

  2. 005.LVM删除

    一 删除LVM步骤 卸载挂载点 移除LV 移除VG 移除PV 删除配置文件 二 卸载挂载点 [root@kauai ~]# umount /dev/vg01/lv01 #先卸载挂载点 三 移除LV [ ...

  3. shell 父子传值

    通过一个中间文件进行: #!/bin/bash   (  subvar="hello shell"  echo "$subvar" > temp.txt ...

  4. 用yum安装JDK

    用yum安装JDK 1.查看yum库中都有哪些jdk版本(暂时只发现了openjdk) [root@localhost ~]# yum search java|grep jdkldapjdk-java ...

  5. 51nod 1584加权约数和

    学到了好多东西啊这题... https://blog.csdn.net/sdfzyhx/article/details/72968468 #include<bits/stdc++.h> u ...

  6. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation 动态规划

    E. Goods transportation 题目连接: http://codeforces.com/contest/724/problem/E Description There are n ci ...

  7. java将文件打包成ZIP压缩文件的工具类实例

    package com.lanp; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

  8. Chrome中使用老的标题栏界面

    Chrome 69中启用了新的UI界面,看着更加秀气了. 但新UI一个不好用的地方是标签栏太高了,留给windows标题栏的空白太小,导致拖动窗口位置非常不方便,如下是一个解决方法: 在地址栏输入: ...

  9. Golang 版本发布 与 TIOBE 排名

    2016年国庆节(10月1日)开始接触 Go 语言,记录一下它的 版本发布 与 TIOBE 排名: Golang 排行榜 月份 版本 排名 备注 2012.03 1.0             201 ...

  10. 【Go命令教程】3. go install

    命令 go install 用于编译并安装指定的代码包及它们的依赖包.当指定的代码包的依赖包还没有被编译和安装时,该命令会先去处理依赖包.与 go build 命令一样,传给 go install 命 ...