0

我需要一些帮助。
我将 int 更改为 hex 之后将其更改为 byte 并尝试将其写入文件。
但是该文件没有作为我构建的jar文件出现在目录中。

 File ModFile =new File(NameText.getText() + ".mod");
FileOutputStream writer = null;

String toProcess = CodesBox.getText();
int i = Integer.parseInt(CodesBox.getText());
byte codes = (byte) i;  

try {
    writer = new FileOutputStream(ModFile);
    writer.write(codes);

} catch (IOException ex) {
    Logger.getLogger(ModMakerGui.class.getName()).log(Level.SEVERE, null, ex);
}finally{
    try {
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

NameText.getText() 肯定在那里, CodesBox.getText() 也绝对正确。

看到这张图片
如您所见,这是我在十六进制编辑器中打开生成的文件时得到的。但我想要这个。
IMG
我可以知道如何解决这个问题吗?
我知道输出文件是带有空格的“vPTP”,我需要空格,谢谢

4

2 回答 2

0

Try this if you use FileOutputStream as well as to write bytes.

File ModFile =new File(NameText.getText() + ".mod");
FileOutputStream writer = null;

try {
    writer = new FileOutputStream(ModFile);
    writer.write(CodesBox.getText().getBytes(),0,CodesBox.getText().getBytes().length);

} catch (IOException ex) {
    Logger.getLogger(ModMakerGui.class.getName()).log(Level.SEVERE, null, ex);
}finally{
    try {
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
于 2012-12-18T09:24:00.607 回答
0

请使用FileWriterPrintWriter

例如:

modFile = new File(NameText.getText() + ".mod");
FileWriter writer = null;
if(!modFile.exists()){
   try {
      modFile.createNewFile();
      writer = new FileWriter(ModFile);
      System.out.println("Mod file has been created to the current directory");
      writer.write(CodesBox.getText());
   } catch (IOException ex) {
      Logger.getLogger(ModMakerGui.class.getName()).log(Level.SEVERE, null, ex);
   }
}
于 2012-12-18T08:01:06.467 回答