package test;import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;public class IoTest { public StringBuffer fileDoc(File path,StringBuffer sb){
//目录列表数组
File[] files = path.listFiles();
//字节流读取
FileInputStream fileInputStream = null;
for(File file1 : files){
if(file1.isDirectory()){//如果是目录
//递归
fileDoc(file1,sb);
}else{
try {
fileInputStream = new FileInputStream(file1);
int size = fileInputStream.available();
byte[] tempByte = new byte[size];
if(fileInputStream.read(tempByte) != size){
System.out.println("文件读取失败!!!");
}else{
//内容转换
String change = new String(tempByte,"utf-8");
sb.append(change);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return sb;
}
//保存数据
public static void saveDataForTxt(StringBuffer sbStr){
//总数据
String allStr = sbStr.toString();
byte[] bytes = allStr.getBytes();
//将数据放到字节输入流中
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
//1k的数据
byte[] newbytes = new byte[1024];
int len = 0;
int i = 1;
try {
while((len = bais.read(newbytes)) != -1){
File file = new File("d:\\java\\"+i+".txt");
new FileOutputStream(file).write(newbytes,0,len);
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String path = "d:\\TDDownload\\Test1";
File file = new File(path);
//要得到的数据集合(初始)
StringBuffer sbTemp = new StringBuffer();
sbTemp = new IoTest().fileDoc(file,sbTemp);
saveDataForTxt(sbTemp);
}
}
解决方案 »
- jdk7与针式打印机的一个问题
- JAVA编程实现矩阵连续相乘问题
- 大家进来看看
- 求助:有没有java高手可以在线指导的?
- 请教:关于JTextPane中插入的文本、Swing组件、图标不整齐的问题,有简化代码分析。
- process中的方法getOutputStream(),得到的输入怎么使用?
- Together For Eclipse 注册码?
- 关于jbuilder 6设置的问题
- redhat7.1上用rpm装jdk1.3.0_01的rpm版,完成后设置/etc/profile,然后运行任何java程序,出现错误:装载libjvm.so出错,没有该文件,各路英雄块救命!
- 【JAVAse的一堆奇葩的问题】
- 帮忙注释一下这个字符串匹配方法,没看懂呀
- Graphics2D求问
while((len = bais.read(newbytes)) != -1){
File file = new File("d:\\java\\"+i+".txt");
new FileOutputStream(file).write(newbytes,0,len);//之前我以为是len的大小问题,后来我-1之后发现没区别
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
public class FileDeal {
private static final int MAX_SIZE = 1024 * 1022;
private static final int BUFFER_SIZE = 4 * 1024;
private static List<File> list = new ArrayList<File>();
private static int currteFileNo = 0; private FileDeal() {
} // 搜索所有文件,放入File
private static void searchAllFiles(File file) {
if (!file.exists()) {
return;
} if (file.isDirectory()) {
for (File f : file.listFiles()) {
searchAllFiles(f);
}
} else {
list.add(file);
}
} /**
* 搜索srcPath文件下的所有文件,合并保存到dstPath目录下,当文件大于1M时,自动分割
*/
public static void writeToFile(String srcPath, String dstPath)
throws IOException {
searchAllFiles(new File(srcPath));
FileInputStream in = null;
FileOutputStream out = null;
byte[] buffer = new byte[BUFFER_SIZE];
try {
File dstFile = new File(dstPath + File.separator + currteFileNo
+ ".txt");
out = new FileOutputStream(dstFile);
for (File f : list) {
try {
in = new FileInputStream(f);
int len = -1;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
out.flush();
//当文件大于1M时,进行分割
if (dstFile.length() >= MAX_SIZE) {
out.close();
out = null;
currteFileNo++;
dstFile = new File(dstPath + File.separator
+ currteFileNo + ".txt");
out = new FileOutputStream(dstFile);
}
}
} finally {
if (in != null) {
in.close();
in = null;
}
}
}
} finally {
if (out != null) {
out.close();
out = null;
}
}
}
}
改进建议:
1. 所有文件内容装入StringBuffer会导致内存溢出。
2. 只有1KB时分割,而不是1M
3. 单个文件大小不足1K时,文件读取、写入会出错。
4. 未对读入的文件流进行关闭。
5. fileDoc方法不需要返回值。
6. File[] files = path.listFiles(); 当path为文件而非文件夹时,将会报空指针异常。
LZ指的是 1Mbyte =1024byte = 1KB 亲,懂了吧?
private static final int MAX_SIZE = 1024 * 1022;
private static final int BUFFER_SIZE = 4 * 1024;
private static List<File> list = new ArrayList<File>();
private static int currteFileNo = 0; private FileDeal() {
} // 搜索所有文件,放入File
private static void searchAllFiles(File file) {
if (!file.exists()) {
return;
} if (file.isDirectory()) {
for (File f : file.listFiles()) {
searchAllFiles(f);
}
} else {
list.add(file);
}
} /**
* 搜索srcPath文件下的所有文件,合并保存到dstPath目录下,当文件大于1M时,自动分割
*/
public static void writeToFile(String srcPath, String dstPath)
throws IOException {
searchAllFiles(new File(srcPath));
FileInputStream in = null;
FileOutputStream out = null;
byte[] buffer = new byte[BUFFER_SIZE];
try {
File dstFile = new File(dstPath + File.separator + currteFileNo
+ ".txt");
out = new FileOutputStream(dstFile);
for (File f : list) {
try {
in = new FileInputStream(f);
int len = -1;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
out.flush();
//当文件大于1M时,进行分割
if (dstFile.length() >= MAX_SIZE) {
out.close();
out = null;
currteFileNo++;
dstFile = new File(dstPath + File.separator
+ currteFileNo + ".txt");
out = new FileOutputStream(dstFile);
}
}
} finally {
if (in != null) {
in.close();
in = null;
}
}
}
} finally {
if (out != null) {
out.close();
out = null;
}
}
}
}
private static final int MAX_SIZE = 1024 * 1022;
private static final int BUFFER_SIZE = 4 * 1024;
private static List<File> list = new ArrayList<File>();
private static int currteFileNo = 0; private FileDeal() {
} // 搜索所有文件,放入File
private static void searchAllFiles(File file) {
if (!file.exists()) {
return;
} if (file.isDirectory()) {
for (File f : file.listFiles()) {
searchAllFiles(f);
}
} else {
list.add(file);
}
} /**
* 搜索srcPath文件下的所有文件,合并保存到dstPath目录下,当文件大于1M时,自动分割
*/
public static void writeToFile(String srcPath, String dstPath)
throws IOException {
searchAllFiles(new File(srcPath));
FileInputStream in = null;
FileOutputStream out = null;
byte[] buffer = new byte[BUFFER_SIZE];
try {
File dstFile = new File(dstPath + File.separator + currteFileNo
+ ".txt");
out = new FileOutputStream(dstFile);
for (File f : list) {
try {
in = new FileInputStream(f);
int len = -1;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
out.flush();
//当文件大于1M时,进行分割
if (dstFile.length() >= MAX_SIZE) {
out.close();
out = null;
currteFileNo++;
dstFile = new File(dstPath + File.separator
+ currteFileNo + ".txt");
out = new FileOutputStream(dstFile);
}
}
} finally {
if (in != null) {
in.close();
in = null;
}
}
}
} finally {
if (out != null) {
out.close();
out = null;
}
}
}
}
eclipse 3.5.2
jdk 1.6.0_25
能通过编译....代码不能通过编译的地反会有提示,请根据提示修改代码。
package a;import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;public class IoTest { public StringBuffer fileDoc(File path, StringBuffer sb) {
// 目录列表数组
File[] files = path.listFiles(); // 字节流读取
FileInputStream fileInputStream = null; for (File file1 : files) {
if (file1.isDirectory()) {// 如果是目录
// 递归
fileDoc(file1, sb);
} else {
try {
fileInputStream = new FileInputStream(file1);
int size = fileInputStream.available();
byte[] tempByte = new byte[size]; if (fileInputStream.read(tempByte) != size) {
System.out.println("文件读取失败!!!");
} else {
// 内容转换
String change = new String(tempByte, "utf-8"); sb.append(change);
} } catch (Exception e) {
e.printStackTrace();
}
}
} return sb;
} // 保存数据
public static void saveDataForTxt(StringBuffer sbStr) {
// 总数据
String allStr = sbStr.toString();
byte[] bytes = allStr.getBytes(); // 将数据放到字节输入流中
ByteArrayInputStream bais = new ByteArrayInputStream(bytes); // 1k的数据
byte[] newbytes = new byte[1024]; int len = 0;
int i = 1;
int cnt = 0;
int totleCnt = 1024 * 1024; // 1M
File file = null; //1、在 try外面定义
FileOutputStream fos = null;
try {
while ((len = bais.read(newbytes)) != -1) {
if (cnt == 0) { //刚开始读取 或者刚读完1M的时候需要重新new一个file 和fos ,写入
file = new File("d:\\java\\" + i + ".txt");
fos = new FileOutputStream(file, true); //2、以追加的模式
}
fos.write(newbytes, 0, len);
cnt += len;
if (cnt == totleCnt) {
cnt = 0 ;
i++;
fos.flush() ; //3 、写完1M,应该把 fos flush() ,然后关闭 ;
fos.close() ;
}
}
} catch (Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
String path = "d:\\Java\\Test1";
File file = new File(path); // 要得到的数据集合(初始)
StringBuffer sbTemp = new StringBuffer(); sbTemp = new IoTest().fileDoc(file, sbTemp); saveDataForTxt(sbTemp);
}
}
这个表达式是java1.5新增的,在java1.4中没有把这句话改为
Java code
for (int i = 0; i < files.length; i++)
{
//for里面的内容中,将file1改成files[i]
......
}结果就通过了!谢谢.