CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2010
    Posts
    0

    Java File Reading Error

    Hello,

    When I am reading a file and printing the contents, it is displaying in some format with control characters (some boxes). I don't know which format it is. I need to convert it into string -- i have replace few values with others according to business logic which is not happening now. Please help.

    Code is as below. The source file is in binary/byte format i guess..


    public void copyFiles(File src, File dest) throws IOException {
    // Check to ensure that the source is valid...
    System.out.println("##### Called Copy Files #######");

    //if (dest.exists()) { JOptionPane.showMessageDialog(null, "Destination already exists"); }

    if (!src.exists()) {
    throw new IOException("copyFiles: Can not find source: "
    + src.getAbsolutePath() + ".");
    } else if (!src.canRead()) { // check to ensure we have rights to the
    // source...
    throw new IOException("copyFiles: No right to source: "
    + src.getAbsolutePath() + ".");
    }
    // is this a directory copy?
    if (src.isDirectory()) {
    if (!dest.exists()) { // does the destination already exist?
    // if not we need to make it exist if possible (note this is
    // mkdirs not mkdir)
    if (!dest.mkdirs()) {
    throw new IOException(
    "copyFiles: Could not create direcotry: "
    + dest.getAbsolutePath() + ".");
    }
    }
    // get a listing of files...
    String list[] = src.list();
    // copy all the files in the list.
    for (int i = 0; i < list.length; i++) {
    File dest1 = new File(dest, list[i]);
    File src1 = new File(src, list[i]);
    copyFiles(src1, dest1);
    }
    } else {
    // This was not a directory, so lets just copy the file
    FileInputStream fin = null;
    FileOutputStream fout = null;
    byte[] buffer = new byte[4096]; // Buffer 4K at a time (you can
    // change this).
    int bytesRead;
    try {
    // open the files for input and output
    fin = new FileInputStream(src);
    fout = new FileOutputStream(dest);
    // while bytesRead indicates a successful read, lets write...
    while ((bytesRead = fin.read(buffer)) >= 0) {
    System.out.println("buffer :"+buffer);

    String value = new String(buffer);
    System.out.println("New buffer :"+value);

    //byte[] encoded = Base64.encodeBase64(buffer);

    String printMe = new String(buffer, "ASCII");
    System.out.println("Latest buffer :"+printMe);


    fout.write(buffer, 0, bytesRead);
    }
    } catch (IOException e) { // Error copying file...
    IOException wrapper = new IOException(
    "copyFiles: Unable to copy file: "
    + src.getAbsolutePath() + "to"
    + dest.getAbsolutePath() + ".");
    wrapper.initCause(e);
    wrapper.setStackTrace(e.getStackTrace());
    throw wrapper;
    } finally { // Ensure that the files are closed (if they were open).
    if (fin != null) {
    fin.close();
    }
    if (fout != null) {
    fout.close();
    }
    }
    }



    }

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Java File Reading Error

    Two things - the code is unreadable because it's not formatted. Please use the CODE tags (see my sig). Also, from what I can make out of the code, it just copies one file into another, which isn't what you described.

    Regarding your question, if you don't know the file format, how can you expect to convert it into anything familiar?

    First, solve the problem. Then, write the code...
    John Johnson
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured