How to read the file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Geethu03
    New Member
    • Dec 2006
    • 45

    #1

    How to read the file

    Hi
    In my project i want to convert the input file to the .doc format. I convert the text file into the .doc file by using the below code

    import java.io.*;

    public class test1 {

    public static void main(String[] args) throws IOException {
    FileInputStream fin;
    int i;
    BufferedReader is = new BufferedReader( new FileReader("C:/tes.txt"));
    try
    {
    try
    {
    fin=new FileInputStream ("C:/tes.txt");
    } catch(FileNotFo undException e) {
    System.out.prin tln("Input file not found");
    return;
    }
    } catch(ArrayInde xOutOfBoundsExc eption e) {
    System.out.prin tln("Usage:Copy file From TO");
    return;
    }
    System.out.prin tln("File Read");
    BufferedOutputS tream bytesOut = new BufferedOutputS tream(new FileOutputStrea m("C:/myFile1.doc"));

    // Copy File

    try
    {
    do
    {
    i=fin.read();
    System.out.prin tln(i);
    if(i != -1)
    bytesOut.write( i);
    }while(i!= -1);
    } catch(IOExcepti on e) {
    System.out.prin tln("File error");
    }
    System.out.prin tln("File Created");

    // Code here to read from is, write to bytesOut

    bytesOut.close( );
    }
    }

    In the same method i also convert the .xls file but in the output the content will be displayed like this

    · Ú 1 È ÿ A r i a l 1 È ÿ A r i a l 1 È ÿ A r i a l 1 È ÿ A r i a l ﷓ "$"#,##0_);\("$ "#,##0\)﷓ ! "$"#,##0_);[Red]\("$"#,##0\)﷓ " "$"#,##0.00_);\ ("$"#,##0.00 \)﷓ ' " "$"#,##0.00 _);[Red]\("$"#,##0.00\) ﷓ 7 * 2 _("$"* #,##0_);_("$"* \(#,##0\);_("$" * "-"_);_(@_)﷓ . ) ) _(* #,##0_);_(* \(#,##0\);_(* "-"_);_(@_)﷓ ? , : _("$"* #,##0.00_);_("$ "* \(#,##0.00\);_( "$"* "-"??_);_(@_) ﷓ 6 + 1 _(* #,##0.00_);_(* \(#,##0.00\);_( * "-"??_);_(@_) à õÿ À à õÿ ô À à õÿ ô À à õÿ ô À à õÿ ô À à õÿ ô À à õÿ ô À à õÿ ô À à õÿ ô À à õÿ

    what can i do to see the original message. The powerpoint files also open in the same condition. Please give the solution to solve the problem.

    Thanks in advance,
    Geethu
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Geethu03
    Hi
    In my project i want to convert the input file to the .doc format. I convert the text file into the .doc file by using the below code

    import java.io.*;

    public class test1 {

    public static void main(String[] args) throws IOException {
    FileInputStream fin;
    int i;
    BufferedReader is = new BufferedReader( new FileReader("C:/tes.txt"));
    try
    {
    try
    {
    fin=new FileInputStream ("C:/tes.txt");
    } catch(FileNotFo undException e) {
    System.out.prin tln("Input file not found");
    return;
    }
    } catch(ArrayInde xOutOfBoundsExc eption e) {
    System.out.prin tln("Usage:Copy file From TO");
    return;
    }
    System.out.prin tln("File Read");
    BufferedOutputS tream bytesOut = new BufferedOutputS tream(new FileOutputStrea m("C:/myFile1.doc"));

    // Copy File

    try
    {
    do
    {
    i=fin.read();
    System.out.prin tln(i);
    if(i != -1)
    bytesOut.write( i);
    }while(i!= -1);
    } catch(IOExcepti on e) {
    System.out.prin tln("File error");
    }
    System.out.prin tln("File Created");

    // Code here to read from is, write to bytesOut

    bytesOut.close( );
    }
    }

    In the same method i also convert the .xls file but in the output the content will be displayed like this

    · Ú 1 È ÿ A r i a l 1 È ÿ A r i a l 1 È ÿ A r i a l 1 È ÿ A r i a l ? "$"#,##0_);\("$ "#,##0\)? ! "$"#,##0_);[Red]\("$"#,##0\)? " "$"#,##0.00_);\ ("$"#,##0.00 \)? ' " "$"#,##0.00 _);[Red]\("$"#,##0.00\) ? 7 * 2 _("$"* #,##0_);_("$"* \(#,##0\);_("$" * "-"_);_(@_)? . ) ) _(* #,##0_);_(* \(#,##0\);_(* "-"_);_(@_)? ? , : _("$"* #,##0.00_);_("$ "* \(#,##0.00\);_( "$"* "-"??_);_(@_) ? 6 + 1 _(* #,##0.00_);_(* \(#,##0.00\);_( * "-"??_);_(@_) à õÿ À à õÿ ô À à õÿ ô À à õÿ ô À à õÿ ô À à õÿ ô À à õÿ ô À à õÿ ô À à õÿ ô À à õÿ

    what can i do to see the original message. The powerpoint files also open in the same condition. Please give the solution to solve the problem.

    Thanks in advance,
    Geethu
    Java does not support .doc or .xls formats that well. To manipulate .doc files you should download and use one of the ready made packages e.g the poi package.

    However, you are also using the wrong classes for doing this. Try using FileWriter and see if the files produced are different.

    Comment

    Working...