Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7947293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:23:26+00:00 2026-06-04T01:23:26+00:00

This is basically what I am trying to do. I wanna take a File

  • 0

This is basically what I am trying to do.

I wanna take a File

Turn it into a Byte Array

Turn it into a String

Store it in a MySQL Table

Retrieve the String

Turn it back into a Byte Array

Turn it back into a File

Now, I have some code for you, which I tried to comment as best as I could. My problem is, that the file I get at the end of this code, doesn’t come out right. It’s missing information. It’s a text file, so I should be able to tell whether the file is complete or not.

As far as I can see, it looks like I only get the last part of the file, and not the entire file. I am pretty sure I messing something up badly somewhere in this conversion. If you got suggestions on how to do this conversion and retrieval more efficiently (Still keeping the Database and all that in mind), please let me know as well!

The code is listed below

import java.io.*;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class main {
    public static void main(String[] args) {
        // The file we want to save.
        File f = new File("build.xml");
        try {
            // Make it into a byte array first
            FileInputStream fis = new FileInputStream(f);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            try {
                for(int readNum; (readNum = fis.read(buf)) != -1;) {
                    bos.write(buf, 0, readNum);
                    System.out.println("read " + readNum + " bytes,");
                }
                StringBuilder s = new StringBuilder();
                // Now we simulate making it into a String, for easier storage
                // in a database.
                for(byte b : buf) {
                    // for debugging
                    s.append(b).append(",");
                    System.out.print(b +",");
                }
                // Now we want to retrieve the file from the database as a string
                File someFile = new File("build2.xml");
                FileOutputStream fos = new FileOutputStream(someFile);
                // We count how many bytes there are in this string.
                // One byte per Token.
                StringTokenizer st = new StringTokenizer(s.toString(),",");
                buf = new byte[st.countTokens()];
                int i = 0;
                StringBuilder t = new StringBuilder();
                // Now we parse out all Bytes from the string, and put them into
                // the prepared byte array.
                while(st.hasMoreTokens()) {
                    byte b = Byte.parseByte(st.nextToken());
                    System.out.print(b + ",");
                    buf[i] = b;
                    i++;
                    // for debugging
                    t.append(b).append(",");
                }
                // Here I print true if both strings are exactly the same
                // which they should be, which means that the bytes are intact
                // before and after conversion.
                System.out.println("\n" +(t.toString().equals(s.toString()) ? true : false));
                // Here we would make the physical file on the machine.
                fos.write(buf);
                fos.flush();
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

http://pastebin.com/699yuE8f

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:23:27+00:00Added an answer on June 4, 2026 at 1:23 am

    Your approach is totally ignoring encodings, which is not a good thing. Characters are not equal to or equivalent to bytes.

    If you have to do it in the sequence you describe, then create the string by something like this:

    String intermediateString = new String(theByteArray,
                                           theSameEncodingTheFileWasCreatedWith);
    

    Likewise, when you convert the string back into bytes, get the bytes like this:

    byte[] bytesToSave = intermediateString.getBytes(theSameEncodingTheFileWasCreatedWith);
    

    But besides any of that, what’s the point of using the string at all? Why not just store the bytes right into the database?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I am trying this, but this only leaves array filled with zeros .
I am basically trying to convert a string similar to this one: 2011-11-9 18:24:12.3
I am trying to to build a Felix bundle in Eclipse. This basically includes
Basically this is a question how to access local scope handler. I trying to
this is what im trying to do... ul#newnav a{ /*--This is basically the hover
I recently came across this in some code - basically someone trying to create
I'm trying to modify the code from this script . Basically I'm trying to
I am having trouble trying to get iterators for inner sub-containers. Basically imagine this
I have tried and failed to get this working. Basically I am trying to
Been trying to get this up and running for a while now. Basically i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.