Java学习笔记——File类之文件管理和读写操作、下载图片
Java学习笔记——File类之文件管理和读写操作、下载图片
File类的总结:
1.文件和文件夹的创建
2.文件的读取
3.文件的写入
4.文件的复制(字符流、字节流、处理流)
5.以图片地址下载图片
文件和文件夹
相关函数
(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");
}
以图片地址下载图片
读取给定图片文件的内容,用FileInputStream
public static byte[] mReaderPicture(String filePath) {
byte[] arr = null;
try {
File file = new File(filePath);
FileInputStream fReader = new FileInputStream(file);
arr = new byte[1024*100];
fReader.read(arr);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return arr;
}
根据byte数组,创建一张新图。
public static void mWriterPicture(String newFileName,byte[] b){
try {
File file = new File(newFileName);
FileOutputStream fStream = new FileOutputStream(file);
fStream.write(b);
fStream.close();
System.out.println("图片创建成功 "+b.length);
} catch (Exception e) {
// TODO: handle exception
}
}
获取指定网址的图片,返回其byte[]
public static byte[] mReaderPictureToInternet(String strUr1){
byte[] imgData = null;
URL url;
try {
url = new URL(strUr1);
URLConnection connection = url.openConnection();
int length = (int)connection.getContentLength();
InputStream is = connection.getInputStream();
if (length!=-1) {
imgData = new byte[length];
byte[] temp = new byte[500*1024];
int readLen = 0;
int destPos = 0;
while ((readLen = is.read(temp))>0) {
System.arraycopy(temp, 0, imgData, destPos, readLen);
//arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
//从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束
destPos+=readLen;
}
}
return imgData;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return imgData;
}
直接获取指定网址的图片
public static void DownPictureToInternet(String filePath,String strUr1){
try {
URL url = new URL(strUr1);
InputStream fStream = url.openConnection().getInputStream();
int b = 0;
FileOutputStream fos = new FileOutputStream(new File(filePath));
while ((b=fStream.read())!=-1) {
fos.write(b);
}
fStream.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
应用:
public static void main(String[] args) {
mWriterPicture("test/1.jpg", mReaderPicture("res/me.jpg"));
mWriterPicture("test/2.jpg", mReaderPictureToInternet(
"http://pic2.desk.chinaz.com/file/201209/7/qinghimingyue4_p.jpg"));
DownPictureToInternet("test/下载.jpg",
"http://img3.100bt.com/upload/ttq/20130205/1360069663700.jpg");
}
Java学习笔记——File类之文件管理和读写操作、下载图片的更多相关文章
- Java学习笔记——File类文件管理及IO读写、复制操作
File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图片 文件和文件夹 相关函数 (boolean) mkdir( ...
- Java学习笔记-File类的基本方法
要渐渐养成写博客的习惯-----> 前段时间看Mars的java中的I/O流没怎么懂,发现I/O流好难啊.今天重新看一遍其他教学,还有书籍,做些笔记,记录下每天的学习生活. File类的一些方法 ...
- java学习一目了然——File类文件处理
java学习一目了然--File类文件处理 File类(java.io.File) 构造函数: File(String path) File(String parent,String child) F ...
- Java学习笔记之---类和对象
Java学习笔记之---类和对象 (一)类 类是一个模板,它描述一类对象的行为和状态 例如:动物类是一个类,动物们都有属性:颜色,动物们都有行为:吃饭 public class Dog { Stri ...
- Java学习:File类
Java学习:File类 File类的概述 重点:记住这三个单词 绝对路径和相对路径 File类的构造方法 File类判断功能的方法 File类创建删除功能的方法 File类获取(文件夹)目录和文件夹 ...
- Java学习笔记:基本输入、输出数据操作实例分析
Java学习笔记:基本输入.输出数据操作.分享给大家供大家参考,具体如下: 相关内容: 输出数据: print println printf 输入数据: Scanner 输出数据: JAVA中在屏幕中 ...
- Java学习之File类理解
File类是io包中唯一代表磁盘文件本身的对象.File类定义了一些与平台无关的方法来操作文件,可以通过调用File类中的方法,实现创建.删除.重命名文件等.File类的对象主要用来获取文件本身的一些 ...
- Java学习:File类中的过滤器接口
javaIO类的File类应用:过滤器接口 FilenameFilter和FileFilter都是用来过滤文件的 例如: 过滤以.jpg或者.java结尾的文件. 通过看他们的源码: 通过使用File ...
- Java学习笔记——SequenceInputStream类合并文件的综合举例分析
SequenceInputStream 介绍 SequenceInputStream 类表示其他输入流的逻辑串联,即文件的合并. 它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾, ...
随机推荐
- load data 方式导入的数据不可以用binlog日志进行恢复,因为binlog里面不产生insert sql语句。
QQ群里面有人问起这个问题: 用load data 导入数据的时候,在binlog文件中记录的不是insert 语句,这样的话,如果用load data 导入数据,当需要恢复数据库的时候 bi ...
- WCF技术剖析之十八:消息契约(Message Contract)和基于消息契约的序列化
原文:WCF技术剖析之十八:消息契约(Message Contract)和基于消息契约的序列化 [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济频道<天天山海经>为此录制 ...
- 『WPF』DataGrid的使用
原文 『WPF』DataGrid的使用 几点说明 这里主要是参考了MSDN中关于DataGrid的说明 这里只会简单说明在WPF中,DataGird最简单的使用方法 对于MSDN中的翻译不会很详细,也 ...
- ActiveMQ持久化方式(转)
消息持久性对于可靠消息传递来说应该是一种比较好的方法,有了消息持久化,即使发送者和接受者不是同时在线或者消息中心在发送者发送消息后宕机了,在消息 中心重新启动后仍然可以将消息发送出去,如果把这种持久化 ...
- hdu2489 Minimal Ratio Tree
hdu2489 Minimal Ratio Tree 题意:一个 至多 n=15 的 完全图 ,求 含有 m 个节点的树 使 边权和 除 点权和 最小 题解:枚举 m 个 点 ,然后 求 最小生成树 ...
- Hadoop: the definitive guide 第三版 拾遗 第十三章 之HBase起步
指南上这一章的开篇即提出:HBase是一个分布式的.面向列的开源数据库.如果需要实时的随机读/写超大规模数据集,HBase无疑是一个好的选择. 简介 HBase 是一个高可靠性.高性能.面向列.可伸缩 ...
- C#利用Lambda和Expression实现数据的动态绑定
在程序开发过程中,有时为了让数据能够实时更新,我们会采用数据绑定来实现. 一般我们数据绑定时我们是这样写的 public class Helper : INotifyPropertyChanged { ...
- Java多线程-实例解析
Java多线程实例 3种实现方法Java中的多线程有三种实现方式:1.继承Thread类,重写run方法.Thread本质上也是一个实现了Runnable的实例,他代表一个线程的实例,并且启动线程的唯 ...
- ubuntu 安装 maven3.2
1.下载并解压 apache-maven-3.2.5-bin.tar.gz tar -xzvf apache-maven--bin.tar.gz 2.设置环境变量 sudo gedit ~/.prof ...
- hdu4709求三角形面积
Herding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...