Examples of how Java reads general text files and Word documents _java

Source: Internet
Author: User

General text files
we 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 (Files). Available ()/1024/1024 + "M"); 
  } catch (FileNotFoundException e) { 
   e.printstacktrace (); 
  } catch (IOException e) { 
   e.printstacktrace (); c30/>}}} 
 
 

. doc files

Here we use Wordextractor to read Word documents, wordextractor from Apache POI Class Library project, official download address: 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); 
 } 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.