Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: reversing text in a file then putting it to a new file

  1. #1
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default reversing text in a file then putting it to a new file

    im having some trouble with this code the document im trying to reverse the text in is about 1000 pages in MS Word and when i use it the whole document doesnt get reversed it does so much then cuts off the rest

    anyone see why it would do that? the program works it just cuts off a lot of the document.

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.RandomAccessFile;
    import java.io.Reader;
    import java.nio.ByteBuffer;
    import java.nio.CharBuffer;
    import java.nio.charset.Charset;
    import java.nio.charset.CharsetEncoder;
     
    public class Test {
     
        public static void main(String... args) throws Exception {
     
            // Stub input. You need to gather it yourself from your sources.
            File file = new File("C:/Documents and Settings/jerky/Desktop/Item.doc");
            long length = file.length(); // Get it from HTTP request header using file upload API in question (Commons FileUpload?).
            String encoding = "UTF-8"; // Get it from HTTP request header using file upload API in question (Commons FileUpload?).
            InputStream content = new FileInputStream(file); // Get it from HTTP request body using file upload API in question (Commons FileUpload?).
     
            // Now the real job.
            Reader input = new InputStreamReader(content, encoding);
            RandomAccessFile output = new RandomAccessFile(new File("C:/Documents and Settings/jerky/Desktop/Item5.doc"), "rwd");
            CharsetEncoder encoder = Charset.forName(encoding).newEncoder();
     
            for (int data; (data = input.read()) != -1;) {
                ByteBuffer bytes = encoder.encode(CharBuffer.wrap(new char[] { (char) data }));
                length -= bytes.limit();
                output.seek(length);
                output.write(bytes.array());
            }
     
            // Should actually be done in finally.
            input.close();
            output.close();
        }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,130
    Thanks
    52
    Thanked 2,286 Times in 2,257 Posts

    Default Re: reversing text in a file then putting it to a new file

    How small a file does the program work with? Is there a file size that it first fails with?
    How large a file is a 1000 pages in MS Word document?

  3. #3
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: reversing text in a file then putting it to a new file

    not sure this is the only file i have tried it with

    and not sure on that but i think its like 5k lines or more easily

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,130
    Thanks
    52
    Thanked 2,286 Times in 2,257 Posts

    Default Re: reversing text in a file then putting it to a new file

    Why are you trying to reverse a doc file?
    The results will be garbage. No program will be able to read it.

    Have you tried debugging the code by adding printlns to report progress as it reads the bytes of the file?

  5. #5
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: reversing text in a file then putting it to a new file

    ah it comes out ok with the stuff that it does reverse it just stops half way through the file or so...i looked at how many lines and its 50,854 lines

    where should i put the println? in the loop? and when it reads?

    could it be that it stops if it hits a character that it cant read?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,130
    Thanks
    52
    Thanked 2,286 Times in 2,257 Posts

    Default Re: reversing text in a file then putting it to a new file

    Why are you trying to reverse a doc file?
    The results will be garbage. No program will be able to read it.

    How many bytes in the input file?
    Have you tried it with a smaller text file? It worked for me for a 2K text file.

    How do you know it stops half way through the file? What do you look at to determine that?

  7. #7
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: reversing text in a file then putting it to a new file

    well its a dat file but i put it in .doc because i wasnt sure if it would read as .dat

    i just tried with a file that is half the size and it worked also

    i look at the text at the beginning of the file and the end of the file and then i match it up where it is in the starting file...i clean some of it and then try again and it gets closer to doing the whole thing each time but still far off

    it started off as a 1.8mb size file now i have it down to 1.5mb

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,130
    Thanks
    52
    Thanked 2,286 Times in 2,257 Posts

    Default Re: reversing text in a file then putting it to a new file

    What does a dat file contain? Text or binary? The extension has no meaning in the I/O classes.

    The UTF-8 CharsetEncoding will not work with binary data like what is in a .doc file.

    I'd recommend you work with a very small file that you can easily verify the results with.
    For good testing, I'd write a simple program that writes out a file with 256 byte values from 00 to 0XFF that you can use as input to your program.

  9. The Following User Says Thank You to Norm For This Useful Post:

    derekxec (July 9th, 2011)

  10. #9
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: reversing text in a file then putting it to a new file

    ahhhh here we go i get this exception

    Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
    at java.nio.charset.CoderResult.throwException(Unknow n Source)
    at java.nio.charset.CharsetEncoder.encode(Unknown Source)
    at com.stackoverflow.q2725897.Test.main(Test.java:30)

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,130
    Thanks
    52
    Thanked 2,286 Times in 2,257 Posts

    Default Re: reversing text in a file then putting it to a new file

    Do you have any answers for post #8?

  12. #11
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: reversing text in a file then putting it to a new file

    sorry i didnt see

    i tried to save the file as .txt with ANSI encoding now and it says it has unicode in it im not that familiar with what unicode is though

    i think thats what ill do write a simple program like you said and use a small file to test cause this is making me crazy haha

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,130
    Thanks
    52
    Thanked 2,286 Times in 2,257 Posts

    Default Re: reversing text in a file then putting it to a new file

    i tried to save the file as .txt with ANSI encoding now
    What is the contents of the file? text or ???
    How are you trying to create/save the file? What program did you use?

  14. #13
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: reversing text in a file then putting it to a new file

    has some text and some weird chars i dont know what they are

    i opened with notepad and saved it as .txt

    here is a bit of whats in the file

     efinK norI eruP2*zb [[[[[[[[[[[[qm qm qm qm   nori erup morf edam efink A  noihclaFȺ2*zb [[[[[[[[[[[[++++?  leets morf edam efink A  noihclaF reppoC20 h [[[[[[[[[[[[qɝ3qɝ3qɝ3qɝ3  reppoc morf edam noihclaF A  noihclaF eznorB20 h [[[[[[[[[[[[++++e  eznorb morf edam noihclaF A
    noihclaF norIȹ20 h [[[[[[[[[[[[++++M  nori morf edam noihclaF A  noihclaF norI eruP20 h [[[[[[[[[[[[''''  nori erup morf edam noihclaF A  sdrowsdaorB leetS20 h [[[[[[[[[[[[#+#+#+#+ *  leets morf edam noihclaF A rebaS reppoCg24 [[[[[[[[[[[[
    &
    &
    &
    &  reppoc morf edam rebaS A rebaS eznorBv24 [[[[[[[[[[[[!*!*!*!*  eznorb morf edam rebaS A
    rebaS norIF24 [[[[[[[[[[[[AAAAa 

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,130
    Thanks
    52
    Thanked 2,286 Times in 2,257 Posts

    Default Re: reversing text in a file then putting it to a new file

    Sorry, I have no idea where you got this file from or what is in the file or what you are trying to do with it or what you expect to happen.

    I guess I'll let you continue to play with whatever it is.

  16. The Following User Says Thank You to Norm For This Useful Post:

    derekxec (July 9th, 2011)

  17. #15
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: reversing text in a file then putting it to a new file

    there is text mixed in there i can delete all the garbage easy i just need it to reverse all the text thats left but it still like 25k lines of text lol

    the file is from a game i play and its an Item list of all the items in the game with a description of what all the items are. i am trying to get the names and descriptions to make a bunch of guides without having to write the 50k lines of info lol

    thanks for all your help ill follow your advice and make the simple program to play around with it

Similar Threads

  1. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  2. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 04:47 AM
  3. Reading a file, then putting it in an array
    By Gondee in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 16th, 2010, 11:11 AM
  4. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  5. Putting text fields in an array
    By Movies32 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 10th, 2010, 06:46 PM