-
Notifications
You must be signed in to change notification settings - Fork 0
/
SafeThread.java
61 lines (54 loc) · 1.63 KB
/
SafeThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package fionaApp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class SafeThread {
public static final int BUFSIZE = 1024 * 32;
public static void main(String[] args) throws IOException {
String folderPath = "D:/PeakAnno/annoSum";
String sumFile = "D:/PeakAnno/annoSum/tdrZp1T.bed";
File folder = new File(folderPath);
File[] fileArray = folder.listFiles();
FileChannel outChannel = null;
FileOutputStream outputStream = new FileOutputStream(sumFile);
outChannel = outputStream.getChannel();
for (int i = 0; i < fileArray.length; i++) {
if (fileArray[i].getAbsolutePath().contains(".bed")) {
getTotalLines(fileArray[i]);
FileInputStream stream = new FileInputStream(fileArray[i]);
FileChannel fc = stream.getChannel();
ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);
while (fc.read(bb) != -1) {
bb.flip();
outChannel.write(bb);
bb.clear();
}
fc.close();
stream.close();
}
}
if (outChannel != null) {
outChannel.close();
}
outputStream.close();
}
private static void getTotalLines(File file) throws IOException {
FileReader filer = new FileReader(file);
LineNumberReader reader = new LineNumberReader(filer);
String strLine = reader.readLine();
int totalLines = 0;
while (strLine != null) {
totalLines++;
strLine = reader.readLine();
}
reader.close();
filer.close();
System.out.println(file.getAbsolutePath() + " : line number = "
+ totalLines);
}
}