How to read a text file using Java

Guys,
Good day!
Please help me how to read a text file using Java and create/convert that text file into XML.
Thanks and God Bless.
Regards,
I-Talk

     public void fileRead(){
             File aFile =new File("myFile.txt");
         BufferedReader input = null;
         try {
           input = new BufferedReader( new FileReader(aFile) );
           String line = null;
           while (( line = input.readLine()) != null){
         catch (FileNotFoundException ex) {
           ex.printStackTrace();
         catch (IOException ex){
           ex.printStackTrace();
     }This code is to read a text file. But there is no such thing that will convert your text file to xml file. You have to have a defined XML format. Then you can read your data from text files and insert them inside your xml text. Or you may like to read xml tags from text files and insert your own data. The file format of .txt and .xml is far too different.
cheers
Mohammed Jubaer Arif.

Similar Messages

  • How to print a text file using Java

    How can I print a text file using Java without converting the output to an image format. Is there anyway I can send the characters in the text file as it is for a print job? I did get a listing doing this ... but that converted the text to an image format before printing....
    THanks,.

    Hi I had to write a print api from scratch, and I did not convert the output to image. Go and read up on the following code. I know there is a Tutorial on Sun about the differant sections of the snippet.
    private void printReport()
         Frame tempFrame = new Frame(getName());
         PrintJob printerJob = Toolkit.getDefaultToolkit().getPrintJob(tempFrame, "Liesltext", null);
         Graphics g = printerJob.getGraphics();
                    //I wrote the method below for calculations
         printBasics(g);
         g.dispose();
         printerJob.end();
    }This alone wont print it you have to do all the calculations in the printBasics method. And as I said I wrote this from scratch and all I did was research first the tutorial and the white papers
    Ciao

  • How to read two text files using the Read from spreadsheet.vi and then plot it

    I need to read two text files using the read from spreadsheet function and then plot these files over time.
    The file with the .res extention is the RMS values (dt and df) and the second file contains the amplitude of a frequency spectrum.
    I really appreciate any help..thanks
    Attachments:
    RMS.txt ‏1 KB
    FREQUENCY.txt ‏1 KB

    From NI Example Finder:
    Write to Text File.vi
    Read from Text File.vi
    Jean-Marc
    LV2009 and LV2013
    Free PDF Report with iTextSharp

  • How to read any text files using file adapter as it is

    Hi,
    I need to build bpel process to read any text files as it is.I am file adapter and using opaque schema.But input file is coming as base64encoding format.But i need the input as it is.How can i do that.
    Is there any sample schema to read input text files as it is?

    in java embedding activity i need to remove all newlines from my input text file.During this process i am getting arryindexoutofbounds exception.Below is my code
    String input = (String)getVariableData("inputFileStr");
    Base64Decoder Decoder = new Base64Decoder();
    try
    String decoded = Base64Decoder.decode(input); --exception is coming at this line
    setVariableData("inputFileStr", decoded);
    decoded = decoded.replaceAll("\\n|\\r", "");
    byte[] outputBytes = decoded.getBytes();
    String reencoded = Base64Encoder.encode(outputBytes);
    setVariableData("outputFileStr", reencoded);
    catch(Exception uee)
    uee.printStackTrace();
    below is stack trace:
    09/02/26 08:10:33 java.lang.ArrayIndexOutOfBoundsException: -1
    09/02/26 08:10:33 at com.collaxa.common.util.Base64DecoderStream.decode(Base64DecoderStream.java:162)
    09/02/26 08:10:33 at com.collaxa.common.util.Base64DecoderStream.decode(Base64DecoderStream.java:143)
    09/02/26 08:10:33 at com.collaxa.common.util.Base64Decoder.decode(Base64Decoder.java:36)

  • How to read a text file using adobe javascript

    Hi,
    I have a api application which adds toolbar to a adobe acrobat. i need to disable the toolbar according to expiry date, So i need to fetch the expiry datge from a text file from the specified location.
    I am using importTextData() function to get the textdata from text file using adobe javascript. When i call this function i am getting error "ReferenceError: importTextData() is not defined".
    I am using Adobe Acrobat 7.0 version.
    Can any one tell how to use the importTextData() function.
    Regards
    Shiva

    calling from javascript file which is placed in the below location.C:\\Program Files (x86)\\Adobe\\Acrobat 7.0\\Acrobat\\Javascripts
    Regards
    Shiva

  • Can't read a text file using java

    Hi All
    I am trying to read a log file.
    ProgramA keeps updating the log file, while my program ProgramB reads it.
    For some readson my ProgramB is not able to read the last two lines in the log file. If I run the program in debug mode it is reading all the lines.
    This is having me frustrated.
    Please let me know if there is a way to read entire contents.
    Here is how I am reading the files ( 2ways)
         private static String readFileAsString(String filePath)
        throws java.io.IOException{
            StringBuffer fileData = new StringBuffer(1000);
            FileReader fr = new FileReader(filePath);
            BufferedReader reader = new BufferedReader(fr);
            char[] buf = new char[1024];
            int numRead=0;
            while((numRead=reader.read(buf)) != -1){
                String readData = String.valueOf(buf, 0, numRead);
                fileData.append(readData);
                buf = new char[1024];
            reader.close();
            fr.close();
            return fileData.toString();
           * Fetch the entire contents of a text file, and return it in a String.
           * This style of implementation does not throw Exceptions to the caller.
           * @param aFile is a file which already exists and can be read.
           static public String readFileAsString(String filePath) {
             //...checks on aFile are elided
             StringBuffer contents = new StringBuffer();
             //declared here only to make visible to finally clause
             BufferedReader input = null;
             try {
               //use buffering, reading one line at a time
               //FileReader always assumes default encoding is OK!
               input = new BufferedReader( new FileReader(filePath) );
               String line = null; //not declared within while loop
               * readLine is a bit quirky :
               * it returns the content of a line MINUS the newline.
               * it returns null only for the END of the stream.
               * it returns an empty String if two newlines appear in a row.
               while (( line = input.readLine()) != null){
                 contents.append(line);
                 contents.append(System.getProperty("line.separator"));
             catch (FileNotFoundException ex) {
               ex.printStackTrace();
             catch (IOException ex){
               ex.printStackTrace();
             finally {
               try {
                 if (input!= null) {
                   //flush and close both "input" and its underlying FileReader
                   input.close();
               catch (IOException ex) {
                 ex.printStackTrace();
             return contents.toString();
           }

    If you're using JDK 5 or later, you could use the Scanner class for input information. See below:
    try {
                   Scanner reader = new Scanner(new File("C:\\boot.ini"));
                   StringBuilder sb = new StringBuilder();
                   while (reader.hasNextLine()) {
                        sb.append(reader.nextLine());
                        sb.append(System.getProperty("line.separator"));
                   System.out.println(sb.toString());
              } catch (FileNotFoundException e) {
                   e.printStackTrace();
              }

  • Read Text file using Java Script

    Hi,
    I am trying to read a text file using Java Script within the webroot of MII as .HTML file. I have provided the path as below but where I am not able to open the file. Any clue to provide the relative path or any changes required on the below path ?
    var FileOpener = new ActiveXObject("Scripting.FileSystemObject");
    var FilePointer = FileOpener.OpenTextFile("E:\\usr\\sap\\MID\\J00\\j2ee\\cluster\\apps\\sap.com\\xapps~xmii~ear\\servlet_jsp\\XMII\\root\\CM\\OCTAL\\TestTV\\Test.txt", 1, true);
    FileContents = FilePointer.ReadAll(); // we can use FilePointer.ReadAll() to read all the lines
    The Error Log shows as :
    Path not found
    Regards,
    Mohamed

    Hi Mohamed,
    I tried above code after importing JQuery Library through script Tag. It worked for me . Pls check.
    Note : You can place Jquery1.xx.xx.js file in the same folder where you saved this IRPT/HTML file.
    <HTML>
    <HEAD>
        <TITLE>Your Title Here</TITLE>
        <SCRIPT type="text/javascript" src="jquery-1.9.1.js"></SCRIPT>
        <script language="javascript">
    function Read()
    $.get( "http://ldcimfb.wdf.sap.corp:50100/XMII/CM/Regression_15.0/CrossTab.txt", function( data ) {
      $(".result").html(data);
      alert(data);
    // The file content is available in this variable "data"
    </script>
    </HEAD>
    <BODY onLoad="Read()">
    </BODY>
    </HTML>

  • How to convert a HTML files into a text file using Java

    Hi guys...!
    I was wondering if there is a way to convert a HTML file into a text file using java programing language. Likewise I would also like to know if there is a way to convert any type of file (excel, power point, and word) into text using java.
    By the way, I really appreciated the help that you guys gave me on my previous topic on how to extract tests from a pdf file.
    Thank you....

    HTML files are already text files. What do you mean you want to convert them?
    I think if you search the web, you can find things for converting those MS Office files to text (or extracting text from them, as I assume you mean).

  • How to Read a CAB File from JAVA?

    Hi,
    Anyone knows how to read a CAB File from java. I need to read a property file of txt file that is packaged in CAB file & then based on that, I have to do processing. Is there anyway to do it.
    I had tried using java.util.zip.ZipFile Class, but it does work for JAR, but not for CAB.
    siva.

    Perhaps there's something in the Cabinet SDK that will help:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncabsdk/html/cabdl.asp

  • How to check & unzip zip file using java

    Dear friends
    How to check & unzip zip file using java, I have some files which are pkzip or some other zip I want to find out the type of ZIp & then I want to unzip these files, pls guide me
    thanks

    How to check & unzip zip file using java, I have
    ve some files which are pkzip or some other zip I
    want to find out the type of ZIp & then I want to
    unzip these files, pls guide meWhat do you mean "other zip"? Either they're zip archives or not, there are no different types.

  • How to read an ARPA file in java

    Hi
    Im new in java and dont know many thing about it, I want to know how can read an ARPA file in java.
    pleas describ it step bye step and esy to understand.
    Edited by: 836719 on Feb 14, 2011 8:15 PM

    836719 wrote:
    ok i explain what i want to do maybe u can help me more,
    i want to read an arpa file and search it to find for example a word.so u think which method that u mentioned above is beter???
    It will be pleasure if u have any tip that can help me.1. Learn the basics of programming as well as how to logically construct and deconstruct a problem domain.
    2. Learn the basics of programming java.
    3. Learn about java.io.*
    4. Learn about java swing
    5. Find documentation that describes the format of the ARPA file.
    6. Read the documentation and understand it.
    7. Find a source for ARPA files (if this is online then there is more java to learn as well.)
    8. Use 1,2 and 3 to write java code that consumes the ARPA file.
    9. Use 4 to ask the user for a word.
    10. Use 1 and 2 to write code that 'searches' what you read in 8.

  • Changing HTML to text file using java

    Hi,
    I am doing a project in which i have to read news articles from websites. I have tried XML but for that i need to know which tag has the article in it. Since i have to read from various websites so each site used different tags for different informations.
    Is there anyway that i can change an HTML file into a text file using java. Maybe some command that removes all the HTML tags and gives just the information. Is there anything else that anyone would like to recommend?
    Thanx
    yafis

    Maybe something like this:
    import java.io.*;
    import java.net.*;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    class GetHTMLText
         public static void main(String[] args)
              throws Exception
              EditorKit kit = new HTMLEditorKit();
              Document doc = kit.createDefaultDocument();
              // The Document class does not yet handle charset's properly.
              doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
              // Create a reader on the HTML content.
              Reader rd = getReader(args[0]);
              // Parse the HTML.
              kit.read(rd, doc, 0);
              //  The HTML text is now stored in the document
              System.out.println( doc.getText(0, doc.getLength()) );
         // Returns a reader on the HTML data. If 'uri' begins
         // with "http:", it's treated as a URL; otherwise,
         // it's assumed to be a local filename.
         static Reader getReader(String uri)
              throws IOException
              // Retrieve from Internet.
              if (uri.startsWith("http:"))
                   URLConnection conn = new URL(uri).openConnection();
                   return new InputStreamReader(conn.getInputStream());
              // Retrieve from file.
              else
                   return new FileReader(uri);
    }

  • How to read several text files at a time

    Dear all
          Read and write one text file is not a problem, but  what confusies me is how to read several text files at one time, in the meanwhile,
    is it possible to display the name of the text file?
    For example, assuming I want to load file" cha 1, cha 2 , cha 3, " at one time and show their names, how to hadle with it
    I have reviewed some files and it is not helpful

    Either with a 'for' loop like in the lib you have attached, or like this attached VI
    that's it
    Message Edited by devchander on 05-30-2006 05:11 AM
    Attachments:
    MULTIPLE READ.vi ‏44 KB

  • How to read a text file through pl/sql

    How to read a text file through pl/sql

    pl/sql runs inside the database. so your file also should be on the database server file system for you to be able to read.
    check out UTL_FILE package. This is the database package to read/write files on the database server.

  • How to read a text file located on other PC.

    Hi,
    i've to read a text file using javabut file is not at my PC but it is on a different PC.I know the IP Address of that PC. But i m not able to connect to it. Plz. provide any suggession with some sample code if possible.
    Thx.

    Do you have access to other host or no? Are you going to write software for both hosts or planning to use existing server?
    Actually you can use any known protocol or write your own.

Maybe you are looking for

  • Blotch On Screen

    Tried to use my 30GB iPod with video this morning and found there's a black splotch (kinda looks like an ink spot) in the upper right corner of the screen. The top third or so of the screen is faded and blank in spots. The bottom half is viewable, bu

  • Jdbc program gives unsupported classVersion error at runtime

    Hello Dear Friends, I have writter a simple java application which fetches some rows from a simple table. I can compile the code without any erros, but while at runtime it gives some error saying that UnsupportedClassVersionError. Please help me. I'm

  • Why has itunes moved my music files out of the Music folder?

    To my horror I just found out that itunes has moved folders (albums) out of the Music folder, Ive always found those folders very well organized (music, movies, podcasts, etc) now Im seeing some albums mixed with those folders (out of the music folde

  • My Album Artwork doesn't display?

    I am not sure what's going on, but my iPod classic doesn't display album art any longer.  It always has in the past.  I am not sure how long it hasn't displayed.  I just noticed the issue.  Any ideas out there?

  • June 2009 affordable HD camcorder suggestions please

    Hi Need to buy an 'affordable' (student budget) HD camcorder to get me going. What suggestions today - 18th June 2009 - and why? Many thanks SC