1

我想将字符串/字符数据作为字节写入文件中。我希望这种转换在 IO.* 类内部发生。我不想在字符串上使用 getBytes() 方法。

我尝试了两个程序,但都将数据写入 Character 。当我在记事本中打开文件时,我可以读取这些字符。如何将我的数据存储为字节?

     import IO.FileWrite;

         import java.io.*;

        public class CharToChar {

private final String data;

public CharToChar(String data){
    this.data = data;
}

public static void main(String[] args) throws IOException {
    final CharToChar charToChar = new CharToChar("I am Manish");
    charToChar.write();
}

private void write() throws IOException {
    final File file = new File("CharToChar.txt");
    final FileWriter fileWriter = new FileWriter(file);
    final BufferedWriter bufferdWriter = new BufferedWriter(fileWriter);
    bufferdWriter.write(this.data);
    bufferdWriter.close();

}
  }


     import java.io.DataOutputStream;
     import java.io.FileOutputStream;
       import java.io.IOException;

       public class WriteStringAsBytesToFile {

public static void main(String[] args) {

    String strFilePath = "WriteStringAsBytes.txt";

    try
    {
        //create FileOutputStream object
        FileOutputStream fos = new FileOutputStream(strFilePath);

  /*
   * To create DataOutputStream object from FileOutputStream use,
   * DataOutputStream(OutputStream os) constructor.
   *
   */

        DataOutputStream dos = new DataOutputStream(fos);

        String str = "This string will be written to file as sequence of bytes!";

   /*
    * To write a string as a sequence of bytes to a file, use
    * void writeBytes(String str) method of Java DataOutputStream class.
    *
    * This method writes string as a sequence of bytes to underlying output
    * stream (Each character's high eight bits are discarded first).
    */

        dos.writeBytes(str);

    /*
     * To close DataOutputStream use,
     * void close() method.
     *
     */

        dos.close();

    }
    catch (IOException e)
    {
        System.out.println("IOException : " + e);
    }

}
   }

注意 - > JAVA 文档说 OutputStreamWriter 一个 OutputStreamWriter 是从字符流到字节流的桥梁: * 写入它的字符使用指定的编码为字节。

4

3 回答 3

5

我认为您对写入字节/字符有错误的概念。字符,只是字节数据的表示。这种表示是根据字符编码类型决定的。看下面的代码:

OutputStream os = new FileOutputStream(filePath);
    os.write("This is byte date".getBytes("UTF-8"));
    os.close();

如果您在运行上述代码段后打开文件,您会注意到文件中有相同的字符串。您的文件始终包含以字节为单位的数据。字节由您的文本编辑器读取并根据其默认字符编码进行编码,通常为UTF-8,但并非总是如此。

于 2013-09-22T12:49:19.923 回答
1

不确定是否有任何 java IO 类可以做到这一点,我看到你不想使用getBytes(),但如果你决定走这getBytes()条路,那么你可以write()在你的第一个例子中修改你的方法,如下所示:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Arrays;

public class CharToChar {

    private final String data;

    public CharToChar(String data){
        this.data = data;
    }

    private void write() throws IOException {       
        OutputStream fos = new FileOutputStream(new File("CharToChar.txt"));
        fos.write(Arrays.toString(this.data.getBytes()).getBytes());
        fos.close();
    }

    public static void main(String[] args) throws IOException {
        final CharToChar charToChar = new CharToChar("I am Manish");
        charToChar.write();
    }
}
于 2013-09-22T09:47:48.077 回答
0

您的要求似乎很奇怪,因为将字符串转换为字节是toBytes您不想使用的方法的工作......但是您可以序列化任何 Java 对象,包括字符串,然后将其保存到文件中,如下所示:

    try ( ObjectOutputStream oos = new ObjectOutputStream(
            new FileOutputStream( "CharToChar.txt" ) ) ) {
        // write any object to a file
        oos.writeObject( "I am Manish" );
        // another option, works for Strings and makes the String pretty much un-readable
        oos.writeBytes( "hello world" );
    } catch ( IOException e ) {
        e.printStackTrace();
    }
于 2013-09-22T09:56:06.937 回答