62,614
社区成员
发帖
与我相关
我的任务
分享
String[] strs = new String[bytess.size()];
strs = bytess.toArray(strs);
/**
* 取工程根目录下所有txt文件进行合并
*/
public void collectionFiles() {
// 取工程根目录下所有txt文件
String[] fileNames = new File("./").list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return "txt".equals(name.substring(name.indexOf(".") + 1).toLowerCase());
}
});
FileOutputStream fos = null;
try {
fos = new FileOutputStream(this.getClass().getName() + ".connection.txt");
for (int i = 0, len = fileNames.length; i < len; i++) {
File f = new File("./" + fileNames[i]);
String title = "\r\n------------" + fileNames[i] + "------------\r\n";
fos.write(title.getBytes("utf8"));
FileInputStream fis = new FileInputStream(f);
byte[] bs = new byte[1 << 11];
int length = 0;
while ((length = fis.read(bs)) > -1) {
fos.write(bs, 0, length);
}
fos.flush();
fis.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
List<Byte> list = new ArrayList<Byte>();
list.add((byte)'0');
list.add((byte)'1');
list.add((byte)'2');
list.add((byte)'3');
list.add((byte)'4');
Byte[] bs = new Byte[list.size()];
list.toArray(bs);
// debug
System.out.println(Arrays.toString(bs));
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(Demo05.class.getName() + ".txt"));
for (int i = 0; i < bs.length; i++)
fos.write(bs[i]);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
当然你可以自己写一个ByteArray类来处理基本类型byte。
还有你可以边读边写呀,没有必要把所有东西读到内存再写文件,这样就可以不借助集合类。
import java.util.ArrayList;
import java.util.List;
/**
* List转数组 -- Java 8
* <p>
* <p>
* <p>
* Created by puruidong on 2015/05/16.
*/
public class Main {
public static void main(String[] args) {
List<Byte> bys = new ArrayList<Byte>();
bys.add(new Byte("1"));
bys.add(new Byte("2"));
bys.add(new Byte("3"));
bys.add(new Byte("4"));
bys.add(new Byte("5"));
System.out.println("转换之前遍历:");
bys.forEach((x)->{
System.out.println(x);});
Byte[] basarray = bys.toArray(new Byte[bys.size()]);
System.out.println("-----------------------\n转换后[输出byte数组]:");
for (int i = 0; i < basarray.length; i++) {
System.out.println(basarray[i]);
}
}
}
List<Byte> bytess = new ArrayList<Byte>();
for(int i = 1;i <= 6;i++){
FileInputStream fis = new FileInputStream(dirpath+i+".txt");
bytess.add((byte) fis.read());
fis.close();
}
//最后bytess写不出了。。。。 输出流都要数组。。。。
最后bytess写不出了。。。。 输出流都要数组。。。。
本来不想用集合的,直接用数组,结果输入流又不能一次性读完- -。。。 要读完也可以- - ,结果发现又要设计数组合并,发现合并java api又没有提供方法还要用循环。。。。
java写个小代码简直累死。