नील कॉफ़ी का समाधान अच्छा है यदि आप निश्चित लंबाई फाइलें पढ़ रहे हैं। हालांकि उन फ़ाइलों के लिए जिनमें परिवर्तनीय लम्बाई है (डेटा आ रहा है) BufferedReader का उपयोग सीधे FileInputStream या FileChannel इनपुटस्ट्रीम पर इनपुट इनपुट स्ट्रीमर के माध्यम से करने में कुछ समस्याएं हैं।
1) आप चाहते हैं पूर्व मामलों पर विचार के लिए कुछ वर्तमान फ़ाइल लंबाई करने के लिए ऑफसेट का डेटा पढ़ने की। तो आप FileInputStream/FileChannel (एक इनपुटस्ट्रीम रीडर के माध्यम से) पर BR का उपयोग करते हैं और इसकी रीडलाइन विधि का उपयोग करते हैं। लेकिन जब आप डेटा पढ़ने में व्यस्त रहते हैं तो कहें कि कुछ डेटा जोड़ा गया है जो बीएफ की पढ़ाई को आपके द्वारा अपेक्षित की तुलना में अधिक डेटा पढ़ने के लिए कारण बनाता है (पिछली फ़ाइल लंबाई)
2) आपने पढ़ा सामान पूरा किया लेकिन जब आप पढ़ने की कोशिश करते हैं वर्तमान फ़ाइल लंबाई/चैनल स्थिति कुछ डेटा अचानक जोड़ा गया है जो वर्तमान फ़ाइल लंबाई/चैनल स्थिति को बढ़ाने का कारण बनता है लेकिन आप इससे पहले ही कम डेटा पढ़ चुके हैं।
ऊपर दोनों मामलों में यह वास्तविक डेटा आप पढ़ लिया है पता करने के लिए
तो (तुम सिर्फ डेटा की लंबाई उपयोग नहीं कर सकते ReadLine का उपयोग करना पढ़ें क्योंकि यह गाड़ी वापसी जैसे कुछ वर्ण को छोड़ देता है) के लिए मुश्किल है buffered बाइट्स में डेटा को पढ़ने के लिए बेहतर है और इसके आसपास एक BufferedReader wrapper का उपयोग करें।मैं इस
/** Read data from offset to length bytes in RandomAccessFile using BufferedReader
* @param offset
* @param length
* @param accessFile
* @throws IOException
*/
public static void readBufferedLines(long offset, long length, RandomAccessFile accessFile) throws IOException{
if(accessFile == null) return;
int bufferSize = BYTE_BUFFER_SIZE;// constant say 4096
if(offset < length && offset >= 0){
int index = 1;
long curPosition = offset;
/*
* iterate (length-from)/BYTE_BUFFER_SIZE times to read into buffer no matter where new line occurs
*/
while((curPosition + (index * BYTE_BUFFER_SIZE)) < length){
accessFile.seek(offset); // seek to last parsed data rather than last data read in to buffer
byte[] buf = new byte[bufferSize];
int read = accessFile.read(buf, 0, bufferSize);
index++;// Increment whether or not read successful
if(read > 0){
int lastnewLine = getLastLine(read,buf);
if(lastnewLine <= 0){ // no new line found in the buffer reset buffer size and continue
bufferSize = bufferSize+read;
continue;
}
else{
bufferSize = BYTE_BUFFER_SIZE;
}
readLine(buf, 0, lastnewLine); // read the lines from buffer and parse the line
offset = offset+lastnewLine; // update the last data read
}
}
// Read last chunk. The last chunk size in worst case is the total file when no newline occurs
if(offset < length){
accessFile.seek(offset);
byte[] buf = new byte[(int) (length-offset)];
int read = accessFile.read(buf, 0, buf.length);
if(read > 0){
readLine(buf, 0, read);
offset = offset+read; // update the last data read
}
}
}
}
private static void readLine(byte[] buf, int from , int lastnewLine) throws IOException{
String readLine = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buf,from,lastnewLine)));
while((readLine = reader.readLine()) != null){
//do something with readLine
System.out.println(readLine);
}
reader.close();
}
private static int getLastLine(int read, byte[] buf) {
if(buf == null) return -1;
if(read > buf.length) read = buf.length;
while(read > 0 && !(buf[read-1] == '\n' || buf[read-1] == '\r')) read--;
return read;
}
public static void main(String[] args) throws IOException {
RandomAccessFile accessFile = new RandomAccessFile("C:/sri/test.log", "r");
readBufferedLines(0, accessFile.length(), accessFile);
accessFile.close();
}
स्रोत
2013-11-08 19:54:40