Rambler's Top100

А Б В Г Д Е Ж З И К Л М Н О П Р С Т У Ф Х Ц Ч Ш Э Ю Я

Все примеры Примеры по пакетам


Чтение файла в буфер, разбивка на строки регулярным выражением


//Чтение файла в буфер, разбивка на строки регулярным выражением
package regexp;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class readLines {
public static CharSequence fromFile(String filename) throws IOException,
FileNotFoundException {
FileInputStream input = new FileInputStream(filename);
FileChannel channel = input.getChannel();
ByteBuffer bbuf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
(int)channel.size());
CharBuffer cbuf = Charset.forName("utf-8").newDecoder().decode(bbuf);
return cbuf;
}
public static void main(String[] args) throws IOException {

CharSequence inputStr = fromFile("/MyDir/readParagraph.java");

String patternStr = "^(.*)$";
Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE);
Matcher matcher = pattern.matcher(inputStr);

while (matcher.find()) {
String lineWithTerminator = matcher.group(0);
String lineWithoutTerminator = matcher.group(1);
System.out.println("line "+lineWithTerminator);
}

}

}

10.01.2009
Rambler's Top100


Ассоциативные ссылки