General text files
Let's take the log file.log file as an example:
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class File_Test { /** * @param args */ public static void main(String[] args) { File file = new File("D://logserrorMsg.log"); if(file.exists()) { System.out.println("This file exists"); } else { System.out.println("This file does not exist"); } try { FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String s; while((s=br.readLine())!=null){ System.out.println(s); } System.out.println("File size is (MB): "+new FileInputStream(file).available() / 1024 / 1024 +"M"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }.doc file
Here we use WordExtractor to read Word documents. WordExtractor comes from Apache's poi library project. The official download address is: https://poi.apache.org/download.html
import java.io.FileInputStream; import org.textmining.text.extraction.WordExtractor; public class WordTest { public static void main(String args[]) throws Exception { new WordTest().readByOther(); } public void readByText() throws Exception { FileInputStream in = new FileInputStream("C://test.doc "); WordExtractor extractor = new WordExtractor(); String str = extractor.extractText(in); System.out.println(str); } }