Problem reading&writing word documents

hi:
i wrote a program to look for a string and replace it with another. reading and wirting are done in the same file. it works fine as long as the file is a text document. if any one has any suggestion on how to make this program work for any file i would appreciate it. here is a portion of the codes that does the reading and wiriting. thanx.
f = new RandomAccessFile(aFile, mode);
          int fLength = (int)f.length();
          int bytesRead;
          byte[] b;// = new byte[fLength];
          String toWrite = "blood suckers";
          String s;
          try{
               b = new byte[fLength];
               while (f.read() != -1)
                    f.read(b);
               s = new String(b);
               System.out.println(s);
               long p =(long)s.indexOf(pattern);               
               String string1 = s.substring((int)p, (int)p+patternLength);
               System.out.println(string1);
               String string3 = s.substring((int)p+patternLength);
               System.out.println(string3);
               toWrite += string3;
               byte[] aByte = toWrite.getBytes();
               f.seek(p+1);
               f.write(aByte);

You can use StringBuffer instead of String because the String constructor use the locale translation and some bytes can be translated in the wrong way.
Or you can write a code which will wolk via the byte array and search for the desired pattern and place the part before the pattern into new byte array (which size is calculated and expanded dynamically), then the string for replacing and after that other part without the search pattern. Repeat it to the end of the original buffer.
The second way is more preferable :-))
Hope this can give you some ideas.
Regards,
Alex

Similar Messages

  • IPad mini for writing word documents?

    Can I use the iPad mini for writing word documents etc. Or even if I can use an app and export it for word etc. Is it already loaded app or do I have to buy it?

    Pages, Keynote and Numbers make up the iWork's suite of apps.
    As long as both iPads are using the same Apple ID for purchases from the App Store you could purchase Pages only once. There are drawbacks for using the same Apple ID on multiple devices when those devices are used by different people.
    Pages will export documents in Word format so that can be opened by Word on a Mac or PC. Pages is a nice app but it is not as all encompassing as Word on your regular computer. There is some formatting that can be done in .word that will not translate in Pages. Even with that though I find Pages a good App.

  • Can JAVA read Microsoft word document?

    Hi
    I am trying to make a program that read a word document, and split the document in to each pages and save each pages with file as it's footer of each pages.
    --- example-----
    test.doc
    Page 1 |
    |
    |
    |
    |
    |
    footer - hi |
    Page 2 |
    |
    |
    |
    |
    |
    footer - test |
    Page 3 |
    |
    |
    |
    |
    |
    footer - still |
    Page 4 |
    |
    |
    |
    |
    |
    footer - hahaha |
    Page 5 |
    |
    |
    |
    |
    |
    footer - lastpage |
    -----------------------> Out put of the program
    hi.doc , test.doc, still.doc, hahaha.doc, lastpage.doc
    Is there any way to import word document and process like that above?
    I have searched about this, and found out 'Jakarta POI' can access to word document but with many restrictions. (I heard it doesn't support word version 2003 and 97)
    Can any one please answer this?
    Thankx

    I'm not really sure that java is the best language to use for this particular project. I haven't looked into writing a standalone application to do this sort of thing, but Microsoft Word has a built in VB editor that allows you to write scripts that will do this very simply, using Word's Document Object Model.
    I would imagine the same sort of the thing could be done from a Stand-alone aplication as well, but you might need some libraries for reading the document and obtaining the object with the same model. I would look into Microsoft's .net solutions for this one. You might even be able to work out a solution in C++, C#, or even J# (which I understand is "similar" to java), if you don't know visual basic.
    It is possible that there is a library out there that will read a Word Document, and create the appropriate Java object with the same object model, but I don't know where it can be found.
    Anyway, if the option is available to you, take a look at Microsoft's .Net. You'll probably have more luck that way. Good Luck!

  • Just updated to Lion.  Didn't realize things like reading a Word document would not be supported.  How do I get these files back and how I do read them going forward?  When a PC user sends me a word document won't I be able to open it and read it?

    Just updated to Lion today, 11/22/11.  I did not realize things likes reading a Word document would not be supported.  "You cannot open this because PowerPC applications are no longer supported."  How am I supposed to read and open Word documents from PC users?  Any insight is helpful, thanks!

    You have Word 2004 which is PPC based. Lion is not able to execute PPC code. You have to upgrade. Or you can do as I do an open them in Pages, since I refuse to allow anything from Microsloth on my Mac.
    Allan

  • Hello! how can i put and read my words documents in the notes pad of my ipod nano 3trd generation?thanks to all.the notes

    hello! how can i put and read my words documents in the notes pad of my ipod nano 3trd generation?thanks to all.

    The Notes features of the 3G iPod Nano only supports TXT files and not Word Documents.  If you want to be able to read these word documents, you'll need to copy the text from each over to a text file.  See here for more information.
    iPod: Storing and reading notes
    Keep in mind that each txt file has a limit of 4096 characters.  See this article for more information as well.
    Long notes are truncated on your iPod
    B-rock

  • API FOR READING MICROSOFT WORD DOCUMENT FILE

    HELLO
    I need to know are there any api's to read a word document i.e a .doc file into a java program. If there r api where can i find them & how can i use them

    For Reading PDF documents, you can use PDFBox, for reading word documents, Apache's POI . But POI supports only Excel right now, Word will soon be supported

  • Can an ipad read microsoft word documents

    Can an iPad read Microsoft Word documents?

    You only need those apps if you wish to create/edit Word documents.

  • Early watch alert - problem with generating word document

    Hello experts,
    since last week I am not able to generate word documents of early watch reports. I have access to 4 different solution manager installations (of customers) where the generation was working without problems but stops since last week.
    SAP gui starts word and the standard word sheet is loaded (SAP early watch start page) but without putting data into the sheet. At the same time I get an error message in the sap gui saying:
    Error during download; read details in long text
    Long text:
    Message no. DSVAS824
    Diagnosis
    The application was unable to write a file to a local directory.
    Procedure
    Often the maximum length of 128 characters for the entire file name, which is defined internally, is exceeded. To avoid this problem, you can change the setting.
    To do this, open Microsoft® Word.
    For English interfaces, choose Tools->Options->File Locations->User templates.
    For German interfaces, choose Extras->Optionen->Speicherort für Dateien->Benutzervorlagen.
    Recommendation: Use directory names with a maximum of 60 characters (For example  C:Program FilesMicrosoft OfficeTemplates)
    I have already maintained the path in word options (we use Word 2007). My colleagues have the same problems.
    Any ideas or hints? Thank you in advance.
    BR, Basti

    Hi all,
    SAP is aware of this problem and is working on a fix.
    For a ***temporary workaround**
    run se38
    RAGS_DSWP_SERV_CONTENT_RESET
    (De-Activate content until  26.05.2011 07:28:06
    Execute
    This will allow you to open the report on a temporary basis.

  • Problem in Downloading Word Document of very huge size

    Hi Folks,
    I upload a word document of 5 MB size to the server. I do hexencoding before
    I upload. The problem is when i try to download the file from the server it
    goes on parsing the document and it does'nt stop. It is getting stuck in
    the while loop. I am using BufferedInputStream for reading the data and
    storing it in a String. Even the CPU utilization stays at 100% in Task Manager.
    Can any of you Suggest a solutation? Should I replace BufferedInputStream with
    some other class.
    Thanks in advance.
    -deena

    No wonder it takes long or even seems that it hangs... Your going to use incredible amount of memory within that loop. First of all you recreate your buffer every time, but then you create new string of it, and then concat it to other, which will again create new string. Stringbuffer will probably help, but it's still a waste.
    try this:
    byte inbuffer[]=new buffer[BUFSIZE];
    byte outbuffer[]=new buffer[BUFSIZE/2];
    int readed;
    while ( (readed=in.read(buffer))!=0)
       hexEncode(outbuffer, inbuffer, readed); // or whatever way you do it
       out.write(outbuffer, 0, readed); // or whereever you want to put the data
    }Anyway the idea is to use common input and output buffer so memory isn't wasted. This is based on a fact that InputStreams read suplied only with byte[] argument read's as much as possible from stream and return the amount of bytes read.

  • Page 5 problem with some word documents (Page 4.3 fine)

    Pages 5.0 (newest version) is unable to open some of my word documents. It is impossible for me to predict which file will cause problem and the error (typical Apple) does ot help at all: "The document cannot be opened". The documents that cannot open with Pages 5.0 open with Word or Pages 4.3 ! Once opened in 4.3, I can copy the content to a blank document in 5.0, save it with no issue but it's a productivity killer!
    Anyone having the same issue?

    Laurent Seroude wrote:
    Apple must deliver a true and ROBUST update to Pages/Numbers/Keynote desktop versions instead of pushing the iCloud/Safari version.
    Good luck with that!
    If you owned iWork '09 you should still have it in your Applications/iWork folder and can use both together on Mavericks.
    Peter
    btw The count of missing features is now over 110 despite Apple reinstating a small number and we haven't bothered to count all the bugs.

  • Problems linking to Word document

    I wrote this as a reply to a similar post last week but I'm
    afraid it got buried in the thread...part of Rick's response to
    that poster was: "2. If you are linking to Word or .PDF documents,
    there is a slight chance you will find the link actually fails for
    some reason. Yet, if you simply place focus in the address bar of
    your browser and press Enter to refresh, the document loads right
    up. Go figure. If that begins happening, let us know and there is a
    possible workaround for you."
    That is the problem I'm having--can you share the workaround
    you mention? It happens most--but not all--of the time, and only
    with the Word documents; my PDFs and web links open with no
    problem. The documents are housed on a public website. I am using
    Captivate 3 if that matters.
    Thank you for any help you can offer!
    Julie

    Hi Julie
    Here's how it's done.
    From inside Captivate 3, click File > Record/Create >
    MenuBuilder Project.
    Choose the Blank Project type and click OK.
    Click Options > Project Options...
    Configure the width and height to the smallest possible
    values. This will be 320 wide and 200 tall.
    Click OK.
    Click Insert > Clickbox.
    Type the file name of the Word document or PDF and ensure you
    click to place a check mark in the "Save file with project" check
    box.
    Click OK and you should see a dialog asking if you want to
    browse for the file. Click No.
    Size the Clickbox so it covers the entire project area.
    Click File > Export.
    Save the project using any name you like. I frequently do
    this and just use "Untitled" as that's what it suggests.
    Choose Flash Movie as the type of project and click Next
    >.
    Type a name to identify the .SWF and choose an easy to
    remember location. I often use the Desktop.
    Click Finish.
    Close MenuBuilder. You are finished with that (unless you
    wish to create more of these)
    In Captivate on the slide where you want the user to click to
    open the document or PDF, click Insert > Animation.
    Browse to the location where you exported the Menubuilder
    file and select it.
    What you are doing is creating a MenuBuilder output to drop
    into a Captivate project to open files. I'm not sure why or how
    it's different than the way Captivate works, but different it is.
    Hopefully this helps you out... Rick

  • Reading Microsoft Word Document in JAVA

    Hi friends,
    im using follwing code to read word document in java using apache poi package..
    import org.apache.poi.poifs.filesystem.*;
    import org.apache.poi.hwpf.*;
    import org.apache.poi.hwpf.extractor.*;
    import java.io.*;
    public class readDoc
    public static void main( String[] args )
    String filesname = "Hello.doc";
    POIFSFileSystem fs = null;
    try
    fs = new POIFSFileSystem(new FileInputStream(filesname;
    //Couldn't close the braces at the end as my site did not allow it to close
    HWPFDocument doc = new HWPFDocument(fs);
    WordExtractor we = new WordExtractor(doc);
    String[] paragraphs = we.getParagraphText();
    System.out.println( "Word Document has " + paragraphs.length + " paragraphs" );
    for( int i=0; i<paragraphs .length; i++ ) {
    paragraphs[i] = paragraphs.replaceAll("\\cM?\r?\n","");
    System.out.println( "Length:"+paragraphs[ i ].length());
    catch(Exception e) {
    e.printStackTrace();
    but im getting exception that
    java.io.IOException: Unable to read entire header; -1 bytes read; expected 512 bytes
    at org.apache.poi.poifs.storage.HeaderBlockReader.<in it>(HeaderBlockReader.java:78)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<i nit>(POIFSFileSystem.java:83)
    how to solve this issue.. please suggest me .. its urgent..
    Add to satheeshtech's Reputation

    You might do better consulting [http://www.nabble.com/Apache-POI-f298.html|http://www.nabble.com/Apache-POI-f298.html] .

  • Small problem reading an XML document

    Hi,
    I have code that can read an XML document like this:
    <?xml version="1.0" encoding="UTF-8"?>
    <something>
    <order orderid="1">
    <customerid>12341</customerid>
    </order>
    </something>
    but when I take out the first tag <something> and last tag </something> , it doesn't work at all and I have no clue why. Here is my code:
    public class SaveOrders extends Object {
          try {
             //Get the root element and all order elements
             Element ordersRoot = ordersDoc.getDocumentElement();
             NodeList orders = ordersRoot.getElementsByTagName("order");
             for (int i = 0; i < orders.getLength(); i++) {
                //For each order, get the order element
                Element thisOrder = (Element)orders.item(i);
          } catch (ClassNotFoundException e) {
             System.out.println("Error creating class: "+e.getMessage());thanks for your help!

    Every XML document must have a Document Root (in your case <something>). So, if you have multiple <order>'s within your XML document, there is no Doc Root! Eeek!
    Get it? If you only have one <order> per XML file, then you should be alright (and might have another problem) because that <order> becomes the root, but the instant you have more than one, you need something to put all of them (your Doc Root).
    Hope that helps!

  • Am I able to use my USB and read saved word documents

    I am wanting to know if I am able to plug in a standard USB storage device and access saved word documents or PDF's. Can this be done via a connector of some sort.
    Katrina

    To expand on what Diavonex has said, here is a good round-up of available devices: http://www.cnet.com/news/wireless-mobile-storage-expander-roundup-frequent-trave lers-youll-want-one-of-these/.
    Also, just understand that these storage devices must be accessed using their own iOS apps and are not generally accessible as you would expect on a computer for example.

  • Problem with open Word Documents and Quark Documents

    Hi,
    I am getting a strange problem with OS X Server 10.4.6, running on a G4 Xserve 1Ghz.
    When working on files in Microsoft Word X and 2004 after having the file open for a few minutes if you try and save we get an error "Word failed reading from this file", or "Bad network connection".
    Also in Quark 6.52 and Quark 6.1 when trying to save a file to the server we get "File not found [-43]". It is possible to open files, and it is also possible to move files onto the share using finder without any problems. To work on documents we are having to work on them locally on the desktop then copy across once done.
    The only thing that i can think has changed is the power went off last night, which casued the Xserve to restart. These problems are occuring across all client computers so i am assuming it can only be a problem with the server.
    Does anyone have any idea what this could be, or any tests that i can do to detmine what is up with it
    Many Thanks
    Tim Pearson

    Hi David,
    I have just tried power-cycling the switches, still getting the same problem.
    I will run disk utility on the shares at lunch. I don't think this will help as it is happening on both data drives.
    I have now also updated the OS X server to 10.4.7, and have turned the access log and error log on, it is showing no errors in the error log, and i can't see anything in the access log that looks out of the ordinary.
    With Quark i can replicate the same problem every time by creating a new project, saving it, altering it and saving it again (this works), then if i alter it again and try and save it comes up with "Duplicate File Name [-48]". It is also not deleting the temporary files, for example "QXP-1055966280.qxp".
    Regards
    Tim Pearson
    Grafika Ltd

Maybe you are looking for