How to access an external text file in a Servlet class?

I do the regular file I/O in the Servlet class. But when I run Servlet class, text file is not loaded. Can sb tell me how to do it?
Thanx
Richard

Hi DrClap;
the following is the main servlet code, which calls other two classes.
===================================================================
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TermsServlet extends HttpServlet {
          public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
          resp.setContentType("text/html");
          PrintWriter out = resp.getWriter();
          out.println("<HTML>");
          out.println("<TITLE>Computer Terms and Acronyms</TITLE>");
          out.println("<BODY>");
          out.println("<H1>Computer Terms and Acronyms</H1>");
          out.println("<TABLE BORDER=2>");
          out.println("<TR><TH>Term<TH>Meaning</TR>");
          TermsAccessor tax = new TermsAccessor("terms.txt");
          Iterator e = tax.iterator();
          while (e.hasNext()) {
               Term t = (Term)e.next();
               out.print("<TR><TD>");
               out.print(t.term);
               out.print("<TD>");
               out.print(t.definition);
               out.println("</TR>");
          out.println("</TABLE>");
          out.println("<HR></HR>");
          out.println("</BODY></HTML>");
=================================================
import java.io.*;
import java.util.*;
public class TermsAccessor {
     protected BufferedReader is;
     protected String ident;
     public TermsAccessor(String inputFileName) throws IOException {
          is = new BufferedReader(new FileReader(inputFileName));
          String ident = is.readLine();
     public String getIdent() {
          return ident;
     public Iterator iterator() {
     return new Iterator() {
          String line, term, defn;
public boolean hasNext() {
          try {
          return ((line = is.readLine()) != null);
          } catch (IOException e) {
          System.err.println("IO Error: " + e);
          return false;
          public Object next() {
          int i;
          while ((i = line.indexOf("\t"))<0 && hasNext());
          if (line == null)
          throw new IllegalStateException("Invalid EOF state");
                    term = line.substring(0, i);
                    defn = line.substring(i+1);
                    return new Term(term, defn);
          public void remove() {
          throw new UnsupportedOperationException();
=============================================================
public class Term {
          public String term;
          public String definition;
          public Term(String t, String d) {
          term = t;
          definition = d;
=================================================================
part of [terms.txt] file as following:
$Id: terms.txt,v 1.5 2000/03/17 04:08:55 $
AMD     American Micro Devices: a chip maker that competes with Intel.
API     Application Programmer Interface: a set of functions that a programmer can use
ASP     Active Server Pages, a Microsoft technology for imbedding certain commands in HTML pages
BSD     Berkeley Software Distribution, one of the two major flavors of UNIX. See OpenBSD
C     A computer programming language invented for writing UNIX in, and very popular in the late 1970's through the 1990's
C++     An Object-Oriented language heavily based on C; recently displaced by Java
CGI     Common Gateway Interface; a script or program used to handle a form on a web page
COBOL     COmmon Business Oriented Language, a programming language used mainly for large-scale financial applications.
CPU     Central Processing Unit, the "chip" that gives a computer its low-level personality ("machine instruction set") and performs individual instructions (add, multiply, compare...). Common CPUs include Intel/AMD "PC", SPARC, Alpha.
=================================================================
the "terms.txt" file and above class files are under the same directory. I use J2EE server to deploy the file, after run, the erro mesage is:"The requested resource (terms.txt (The system cannot find the file specified)) is not available." That means server can not locate the terms.txt file. So what file path should I use?
Thanx
Richard

Similar Messages

  • How to load external text file into a Form?

    Hi,
    I made a menu with 1-5 in a Form and 5 external text files. I want the user who is able to view the text content of the text file when he chooses one of them from the menu. And the text file screen has a "Back" command button to let him go back.
    How can I do it and can it support other languages such as Chinese and Japanese?
    Thanks for help.
    gogo

    Sorry, I made the mistake about the subject, it should be loading local file, not external file through http.
    I wrote a method but it throwed an exception when the midlet was run.
    private void loadText()
    try {
    InputStream is = this.getClass().getResourceAsStream("/text.txt");
    StringBuffer sb = new StringBuffer();
    int chr;
    while ((chr = is.read()) != -1)
    sb.append((char) chr);
    is.close();
    catch (Exception e)
    System.out.println("Error occurs while reading file");
    I put the text.txt file in the same folder with the main file (extends midlet). How can I load the text content and display it on StringItem?
    Thanks for any help.
    gogo

  • How to write data to text file using external tables

    can anybody tell how to write data to text file using external tables concept?

    Hi,
    Using external table u can load the data in your local table in database,
    then using your local db table and UTL_FILE pacakge u can wrrite data to text file
    external table
    ~~~~~~~~~~~
    http://download-east.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_7002.htm#i2153251
    UTL_FILE
    ~~~~~~~~~
    http://download-east.oracle.com/docs/cd/B19306_01/appdev.102/b14258/u_file.htm#sthref14093
    Message was edited by:
    Nicloei W
    Message was edited by:
    Nicloei W

  • Find Change through external text file

    Hello folks
    I am bit pretty in InDesign scripting so could you please look into this.
    How can i change any particular text field in Indesign CS3 document from text file.
    I do have find change script but for each InDesign document specific text file is assigned.
    So each time i have to modify find change GREP property that is also repetetive of work. Is there any way to get find change information should be extract from external text file.
    Many Tanks in advance

    In the FindChangeByList script, you could customize the function myFindFile(myFilePath) {...} as to search the FindChangeList text file in the document location rather than the script location. That's an example. The question is: given a document, where will you have the corresponding FindChangeList?
    @+
    Marc

  • Appending an external text file

    Hi everyone. I need a little help. I'm trying to append an external text file. I'm able to read it using the "in.readLine()" and "BufferedReader" commands but can't figure how to append it. The reason being is I'm trying to add new information to each new line. For example my ext. text file looks something like this:
    Homer Simpson 3489 Evergreen Terrace Springfield
    Marge Simpson 3489 Evergreen Terrace Springfield
    Bart Simpson 3489 Evergreen Terrace Springfield
    Please note that I'm not using a string tokenizer to read the text file. If someone could please give me an example to append information to each new line I would greatly appreciate it. Also please note that the text file is being read into a vector to hold the information. Thank you.

    open the FileWriter or FileOutputStream you use in "append" mode..
    http://java.sun.com/j2se/1.4.1/docs/api/java/io/FileOutputStream.html
    FileOutputStream(String name, boolean append)
              Creates an output file stream to write to the file
              with the specified name.http://java.sun.com/j2se/1.4.1/docs/api/java/io/FileWriter.html
    FileWriter(String fileName, boolean append)
              Constructs a FileWriter object given a file name
              with a boolean indicating whether or not to append
              the data written.

  • How do I get a text file from Photoshop  to work in the main sequence in pp?

    How do I get a text file to work properly in the master sequence. I moved it from Photoshop, which I learned to do from a tutorial, but when I move the animated text sequence to the master, it either isnt running, or it is scaled way too big. How do I get it to run in the main sequence?

    "Wont Work Here" !  Does not mean much.
    Are you having an audio or a video issue? 
    Looks like no video clip on the video layer above that section of audio.
    I am teaching myself this stuff completely on the fly
    I suggest you do the Basic Tutorials ( Adobe TV for example) in both Premiere Pro and PhotoShop.
    You need to be competent in the basics and fundamentals of these apps and that will also help you describe and discuss the issues.   Check the 'Products on this site....
    Adobe TV

  • How to read a whole text file into a pl/sql variable?

    Hi, I need to read an entire text file--which actually contains an email message extracted from a content management system-- into a variable in a pl/sql package, so I can insert some information from the database and then send the email. I want to read the whole text file in one shot, not just one line at a time. Shoud I use Utl_File.Get_Raw or is there another more appropriate way to do this?

    how to read a whole text file into a pl/sql variable?
    your_clob_variable := dbms_xslprocessor.read2clob('YOUR_DIRECTORY','YOUR_FILE');
    ....

  • Add one character at  a time from External Text file

    Hi,
    I'm using Flash 8, and I have created a standard Dynamic
    field that pulls the text from an external text file using the
    LoadVars function and it works fine.
    To get the correct effect for the design, I would like to be
    able to create the illusion that the characters are being typed out
    when the page loads as if someone has started typing and also hear
    the "Typewriter Clack" each time a character appears.
    The only place I have found an example is on the Syphon
    Filter Game website which seems to have created the illusion
    perfectly:
    http://www.syphonfilterdarkmirror-thegame.com/en_IE/
    (Check out the bottom right section)
    I could create a Movieclip and add a character each frame
    but, I would like to be able to edit the text when required via a
    text file or even ASP or PHP if neccessary.
    Any help would be grateful.
    Cheers

    or here:
    http://www.actionscripts.org/tutorials/beginner/Scripted_Typerwriter/index.shtml

  • How to read the whole text file lines using FTP adapter

    Hi all,
    How to read the whole text file lines when error occured middle of the text file reading.
    after it is not reading the remaining lines . how to read the whole text file using FTP adapter
    pls can you help me

    Yes there is you need to use the uniqueMessageSeparator property. Have a look at the following link for its implementation.
    http://download-west.oracle.com/docs/cd/B31017_01/integrate.1013/b28994/adptr_file.htm#CIACDAAC
    cheers
    James

  • How to scan/ read a text file, pick up only lines you wanted.

    I'm new to Labview, I trying to wire a VI which can read a text file
    example below:
    Example: My Text File:
    1 abcd
    2 efgh
    8 aaaa
    1 uuuu
    and pick out only any line which start with number 1 (i.e 1 abcd)
    then output these 2 lines out to a a new text file?
    Thanks,
    Palmtree

    Hi,
    How do you creat your text files? Depending on the programm, there is added characters in the beginning and at the and of the file. So here you will find a VI to creat "raw" text files.
    To debug your VIs, use the probs and breakpoints, so you can solve the problem by your self or give an more accurate description of it.
    There is also the VI that read a integer and two strings  (%d %s %s)
    Attachments:
    creat text file.vi ‏9 KB
    read_from_file.vi ‏14 KB

  • How may load an external (html) file in swf? Anyone!!!!!

    hi all,
        How may load an external html file in swf? such as google.com in a movieclip.
    anyone may help me.........????????

    With Flash, you can't do this.
    Just with FlashBuilder and Air (something like HTMLLoader  , not sure now)
    I mean loading in a movieclip..
    Of course you can open a page to display in a browser.

  • How do I open a text file to sort using bubble sort?

    Hi,
    I have a text file, which is a list of #'s.. ex)
    0.001121
    0.313313
    0.001334
    how do you open a text file and read a list? I have the bubble sort code which sorts.. thanks

    I wrote this, but am getting a couple errors:
    import java.util.*;
    import java.io.*;
    public class sort
         public static void main(String[] args)
              String fileName = "numsRandom1024.txt";
              Scanner fromFile = null;
              int index = 0;
              double[] a = new double[1024];Don't use double. You should use something that implements Comparable, like java.lang.Double.
              try
                   fromFile = new Scanner(new File(fileName));
              catch (FileNotFoundException e)
    System.out.println("Error opening the file " +
    " + fileName);
                   System.exit(0);
              while (fromFile.hasNextLine())
                   String line = fromFile.nextLine();
                   a[index] = Double.parseDouble(line);Don't parse to a double, create a java.lang.Double instead.
                   System.out.println(a[index]);
                   index++;
              fromFile.close();
              bubbleSort( a, 0, a.length-1 );No. Don't use a.length-1: from index to a.length-1 all values will be null (if you use Double's). Call it like this:bubbleSort(a, 0, index);
    public static <T extends Comparable<? super T>> void
    d bubbleSort( T[] a, int first, int last )
              for(int counter = 0 ; counter < 1024 ; counter++)
    for(int counter2 = 1 ; counter2 < (1024-counter) ;
    ) ; counter2++)Don't let those counter loop to 1024. The max should be last .
                        if(a[counter2-1] > a[counter2])No. Use the compareTo(...) method:if(a[counter2-1].compareTo(a[counter2]) > 0) {
                             double temp = a[counter2];No, your array contains Comparable's of type T, use that type then:T temp = a[counter2];
    // ...Good luck.

  • How to run an external .exe file from an indesign pluging

    Hi,
          Suppose if I have written an separate application in C++ (.exe file) & need to run it from an indesign pluging(as if a service in windows). have you provided that facilities in your SDK? if it's please let me know how to run an external .exe file from a indesign pluging.
    Thanks,

    I'm actully writing data in PMString to a external txt file.
    another question..
    if i want to execute an action when the ok button is cliked how can i do it?
    whe i add a button(widget) i know how to handle it. please see my code.
    // Call the base class Update function so that default behavior will still occur (OK and Cancel buttons, etc.).
    CDialogObserver::Update(theChange, theSubject, protocol, changedBy);
    do
    InterfacePtr<IControlView> controlView(theSubject, UseDefaultIID());
    ASSERT(controlView);
    if(!controlView) {
    break;
    // Get the button ID from the view.
    WidgetID theSelectedWidget = controlView->GetWidgetID();
    if (theChange == kTrueStateMessage)
    //if (theSelectedWidget == kEXTCODGoButtonWidgetID
    switch(theSelectedWidget.Get())
             case kEXTCODGoButtonWidgetID:
      this->ViewOutput();
      break;
             case kEXTCODFindButtonWidgetID:
      this->SaveLog();
      break;
    // TODO: process this
    } while (kFalse);
    I do two actions "SaveLog" & "ViewOutput()" using two buttons. But i dont know how to execute an action when the ok button is clicked...

  • How to append paragraph in text file of TextEdit application using applescript

    how to append paragraph in text file of TextEdit application using applescript and how do i save as different location.

    christian erlinger wrote:
    When you want to print out an escape character in java (java is doing the work in client_text_io ), you'd need to escape it.
    client_text_io.put_line(out_file, replace('your_path', '\','\\'));cheersI tried replacing \ with double slash but it just printed double slash in the bat file. again the path was broken into two lines.
    file output
    chdir C:\\DOCUME~1\
    195969\\LOCALS~1\\Temp\
    Edited by: rivas on Mar 21, 2011 6:03 AM

  • I have recently purchased a new computer and photoshop element. It looks like its downloaded it but I don't know how to access it. it says file are ready...down load files have been extracted and saved to folder....launch PS elements and open specific fol

    I have recently purchased a new computer and photoshop element. It looks like its downloaded it but I don't know how to access it. it says file are ready...down load files have been extracted and saved to folder....launch PS elements and open specific folder. it looks like it downloads. It then keeps taking me back to this page. Im not sure where to go next

    if you have a win os you should have dl'd an exe and a 7z file.
    put both in the same directory and double click the exe.

Maybe you are looking for

  • Looking for Quicktime version 7.1.5?

    I'm suffering the same frustrations with the Quicktime/iTunes issue. My iTunes was accidently deleted off my computer and I'm trying to reinstall everything again (iTunes, Quicktime, etc.) with many road blocks. I'm following advise posted to reinsta

  • Application Process Issue

    +(First off, we're using ApEx 4.0 through IE8)+ I have a report that serves as a 'shopping cart' of sorts, with a 'Remove' button to cancel items from it. But the 'Remove' button doesn't go to the page, it just leaves a blank white page. (Thankfully,

  • Images in all programs are reddisch

    If you draw a picture in the length or larger, the image turns reddish. it is in all programs.

  • IDoc Step by Step

    Hello Everybody, Can any body provide me steps to generate IDoc for ALE all the steps from initial.how to configure,define Idoc,Message type, RFC.In sort whole cycle from Sender to Reciever.If possible give one example. Thanks, RP

  • Property Loader won't Load FileGlobal variable values

    What am I doing wrong here? I've Exported three FileGlobal string variables into a tab delimited TEXT file and then cleared out the Values (values="") . The contents of that file is: Variable Value Variable Value FilteredInputValueTag "tag_AI_ENGINEE