知识点:文件读写 (字符流和字符流),属性文件的读取与文件的切割合并
来源:互联网 发布:郑州大学网络教育学费 编辑:程序博客网 时间:2024/06/06 18:58
import java.io.*;//我自己的代码都是写具体的导入哪些类,但在这里有点过长,于是我换了*
/**
* 文件读写 (字符流和字符流),通过键盘写入,输出到终端,读出文件,写到硬盘(文件),对一个非文本文件进行复制
* @author Administrator
*/
public class Text12 {
public static void keyBoard() {
//通过键盘写入,输出到终端(由键盘输入可不释放资源,但必刷)
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String str = null;
try {
while((str = br.readLine()) != null) {
try {
bw.write(str.toUpperCase());
bw.newLine();
bw.flush(); //必刷
} catch (IOException e) {
new RuntimeException("写入出错");
}
}
} catch (IOException e) {
new RuntimeException("读出错");
}
}
//读出文件,写到硬盘(文件)
public static void file() {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader("Text11.java"));
} catch (FileNotFoundException e1) {
new RuntimeException("找不到文件");
}
try {
bw = new BufferedWriter(new FileWriter("Text11.txt"));
} catch (IOException e1) {
new RuntimeException("新建出错");
}
String str = null;
try {
while((str = br.readLine()) != null) {
try {
bw.write(str.toUpperCase());
bw.newLine();
bw.flush(); //必刷
} catch (IOException e) {
new RuntimeException("写入出错");
}
}
} catch (IOException e) {
new RuntimeException("读出错");
}
}
//对一个非文本文件进行复制
public static void music() {
BufferedInputStream is = null;
BufferedOutputStream os = null;
int ch;
try {
is = new BufferedInputStream(new FileInputStream("Chen - The Best Luck.mp3"));
} catch (FileNotFoundException e) {
new RuntimeException("找不到文件");
}
try {
os = new BufferedOutputStream(new FileOutputStream("Chen - The Best Luck3.mp3"));
} catch (FileNotFoundException e) {
new RuntimeException("新建音乐失败");
}
try {
while((ch = is.read()) != -1) {
os.write(ch);
//os.flush();不一定要刷新
}
} catch (IOException e) {
new RuntimeException("写入音乐失败");
}
}
public static void main(String args[]) {
//keyBoard();
//file();
music();
}
}
思考与感悟:
字符流跟字节流都是比较死的东西,熟悉了就好了。当然不要忘了关流(关闭与流相关的系统资源,你的流对象,垃圾回收器会帮你回收的,你放心),还有刷新,因为数据都是通过先写到缓存的流对象里面再写到硬盘的,你要是怕麻烦就用PrintWriter类好了,它自动给你刷新。还有换行。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
/**
* 文件递归查询,属性类(软件设定5次就不能用了),切割合并文件
* @author Administrator
*
*/
public class Text13 {
//文件递归查询
public static void showFile(File dir) {
if (dir.isDirectory()) {
System.out.println( dir.getName());
}
File[] file = dir.listFiles();
for (File f:file) {
if (f.isDirectory()) {
showFile(f);
}
else
System.out.println("|--" + f.getName());
}
}
//属性类(软件设定5次就不能用了)
public static void property() {
File file = new File("F:\\黑马\\Java基础知识\\count.property");
FileInputStream is = null;
FileOutputStream os = null;
Properties pro = new Properties();
int count = 0;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e1) {
new RuntimeException("新建的输入流失败");
}
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
new RuntimeException("新建文件错误");
}
}
try {
pro.load(is);
} catch (IOException e) {
new RuntimeException("属性列表加载错误");
}
String value = pro.getProperty("time");
if (value != null) {
count = Integer.parseInt(value);
}
count = count + 1;
if (count >= 5) {
System.out.println("不能再用了");
}
pro.setProperty("time", count + "");
//输出流的位置很重要,it.hasNext();在取count前新建的话,会在count自增之前把旧的的文件覆盖,这样count永远为1
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
new RuntimeException("新建的输出流失败");
}
try {
pro.store(os, "time");
} catch (IOException e) {
new RuntimeException("存储错误");
}
}
//切割文件
public static void splitFile() {
//jepg即jpg
File file = new File("E:\\ldx.jpg");
FileInputStream is = null;
FileOutputStream os = null;
byte[] buf = new byte[1024*20];
int len = 0;
int count = 0;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
new RuntimeException("新建的输入流失败");
}
try {
while((len = is.read(buf))!= -1) {
count++;
os = new FileOutputStream("E:\\" + count + ".part");
os.write(buf, 0, len);
}
} catch (IOException e) {
new RuntimeException("读数据失败");
}
}
//合并文件
public static void combineFile() {
byte[] buf = new byte[1024*20];
FileOutputStream os = null;
int len = 0;
ArrayList<FileInputStream> is = new ArrayList<FileInputStream>();
try {
is.add(new FileInputStream("E:\\1.part"));
is.add(new FileInputStream("E:\\2.part"));
is.add(new FileInputStream("E:\\3.part"));
is.add(new FileInputStream("E:\\4.part"));
is.add(new FileInputStream("E:\\5.part"));
} catch (FileNotFoundException e) {
new RuntimeException("新建的输入流失败");
}
//为什么老师这里不是final,而我这里要。
final Iterator<FileInputStream> it = is.iterator();
Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() {
@Override
public boolean hasMoreElements() {
return it.hasNext();
}
@Override
public FileInputStream nextElement() {
return it.next();
}
};
SequenceInputStream sis = new SequenceInputStream(en);
try {
os = new FileOutputStream("E:\\1.jpg");
} catch (FileNotFoundException e) {
new RuntimeException("新建的输出流失败");
}
try {
while((len = sis.read(buf))!=-1) {
os.write(buf, 0, len);
}
} catch (IOException e) {
new RuntimeException("读数据失败");
}
try {
if (sis != null)
sis.close();
} catch (IOException e) {
new RuntimeException("合并流关闭失败");
}
try {
if (os != null)
os.close();
} catch (IOException e) {
new RuntimeException("输出流关闭失败");
}
}
public static void main(String args[]) {
/*File dir = new File("F:\\黑马");
showFile(dir);*/
//property();
//splitFile();
combineFile();
}
}
思考与感悟:
Properties就是一些写入取出。文件类型这个有必要说一下。我们要习惯把路径设成文件类型,这样可以给我们提供很多便利。因为File有很多实用方法。
文件切割就没啥的,也没用到什么关键的类,就是读的时候分块放到不同的小块文件里而已。我们的read方法本身就可以把数据暂存到一个Byte[]里面。
合并的话用到了SequenceInputStream类,这个类又用到了Enumeration类。这些都是这样用的。你明白为什么但是你还是没想到有什么更简单的方式。
- 知识点:文件读写 (字符流和字符流),属性文件的读取与文件的切割合并
- 字符流的文件读取
- 文件的切割与合并
- 文件的切割与合并
- 字符流读写文件
- 字符流缓冲区实现文件的读取
- 字符流读取一个文件的内容
- Java之基于字符流的文件写入与读取
- IO流 应用: 文件的 切割与合并
- 黑马程序员-文件的切割与合并
- 文件的切割与合并练习
- 大文件的切割与合并
- Linux大文件的切割与合并
- SequenceInputStream 实现文件的切割与合并
- File类文件的切割与合并
- IO流__【概述】【字符文件的读写和复制】【字符流缓冲区】
- python读写文件,和设置文件的字符编码
- 【java】:对属性文件的读写操作,中文字符解决方案
- C中malloc的使用
- Pthreads多线程编程(1)
- Android学习——AsyncQueryHandler
- 不能从const char *转换为LPCWSTR 经常碰到
- 搬离百度空间
- 知识点:文件读写 (字符流和字符流),属性文件的读取与文件的切割合并
- POJ - 1006 Biorhythms (中国剩余定理)
- error C2061: 语法错误: 标识符“__RPC__out_xcount_part” VS2010
- HDU 2639 Bone Collector II 背包k优解
- linux学习笔记4----vim编辑器
- java.lang.IllegalStateException: You cannot call onTextChanged with a non filterable adapter
- 3.3-多个栈组合模拟一个栈
- struts1源码学习6(doPost和doGet)
- 如何调用DLL