2012-05-17 46 views
1

这基本上是我想要做的。文件 - >字节[] - >字符串 - >字节[] - >文件转换

我想带一个文件

把它变成一个字节数组

把它变成一个字符串

储存于一个MySQL表

检索字符串

打开它回到一个字节阵列

T它回到一个文件

现在,我有一些代码给你,我试图评论尽我所能。我的问题是,我在这段代码的末尾得到的文件没有出来。它缺少信息。这是一个文本文件,所以我应该能够判断文件是否完整。

据我所见,它看起来像我只得到文件的最后部分,而不是整个文件。我很确定我在这个转换中的某个地方糟糕透顶。如果您对如何更有效地完成此转换和检索(仍然保留数据库及所有内容)有任何建议,请让我知道!

的代码下面列出

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

+2

将来,请将代码直接发布到您的问题中。 –

+0

我总是弄错了,然后格式看起来很可怕。 – OmniOwl

+4

使用代码示例将其与文本区分开来。 – Venki

回答

2

你的方法完全忽略编码,这不是一件好事。字符是而不是等于或等于字节。

如果你必须这样做你所描述的顺序,然后像这样创建的字符串:

String intermediateString = new String(theByteArray, 
             theSameEncodingTheFileWasCreatedWith); 

同样,当你的字符串转换回字节,得到这样的字节:

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

但除此之外,使用字符串有什么意义呢?为什么不直接将字节存储到数据库中?

+0

我以前从来没有在数据库中存储字节,所以我想尝试将它存储为更熟悉的东西,用于狗屎和笑声。但是我之前已经把一个字节数组转换成了一个文件,而不必考虑编码,所以..yeah。 – OmniOwl

+0

将字节数组保存为文件时,您无需关心编码。字节是字节。当字符串处于进程中间时,需要进行编码。字节 - >字符串和字符串 - >字节意味着你必须处理编码。 – QuantumMechanic

+0

@Vipar:它试图存储什么样的字节有很大的不同。 QuantumMechanic是正确的,'char'!='byte'!您是否试图将*文件中的文本*存储到数据库中,或者您是否试图存储*文本数据以使您能够重新构建文件*?如果你想要后者,为什么不把实际字节存储为BLOB? –

1

您只需搞砸字符串创建的,你不看bosbuf

  for(byte b : >>buf<<) { 
       // for debugging 
       s.append(b).append(","); 
       System.out.print(b +","); 
      } 

否则我不相信它会起作用或者它是一个很好的解决方案。为什么你不能把它存储在数据库中?

+0

因为我想将它作为纯文本存储,而不是物理文件。 – OmniOwl

+1

@Vipar:将任意二进制数据存储为字符串是一个众所周知的问题。正常的解决方案是使用[base64](http://en.wikipedia.org/wiki/Base64)之类的东西,而不是发明自己的编码。 –

0

您共享的代码是恕我直言,因为它必须是更复杂。

如果你只是对字符串表示感兴趣,你为什么要在字节级读取文本?

我倾向于使用InputStreamReader来阅读文件。这可以让你直接操作角色。

+0

不需要苛刻。我只是在黑暗中拍摄。感谢您的输入。 – OmniOwl

+0

我不确定你为什么认为我的回答很糟糕。我虽然直接回答,但直接让你明白这一点。代码越长,代码编写者获得的更多可能性就越小。 – Robert

相关问题