2014-10-31 69 views
1

我正在使用write()方法写入外部存储的文件。该方法只接受byte []作为输入。我曾尝试传递一个字符串,并且我收到一条错误消息(“类型FileOutputStream中的方法write(int)不适用于参数String”)。如果我传递一个int,我不会收到错误,但是在文件中没有写入任何内容。我从调用getNumSentPackets()得到的值是一个int,我需要将它转换为byte []。我一直在寻找已经在这里回答的其他问题,并且我尝试了ByteBuffer选项,但是我在文件中得到的结果并不是我想要的,这意味着我没有收到发送数据包的数量。有人可以帮我吗?在Android中将int转换为byte []

这是我的代码:

 public void createFile(String name) { 

    try { 
     String filename = name; 
     File myFile = new File(Environment.getExternalStorageDirectory(), filename); 
     if (!myFile.exists()) 
     myFile.createNewFile(); 
     String title = "FLOODING RESULTS FILE\n\n"; 
     String sent = "Number of sent packets\n"; 
     FileOutputStream fos; 
     byte[] data = title.getBytes(); 
     byte[] intSent = sent.getBytes(); 
     int numSent = mSender.getNumSentPackets(); 
     byte[] numSentBytes = ByteBuffer.allocate(10).putInt(numSent).array(); 
     try{ 
     fos = new FileOutputStream(myFile); 
     fos.write(data); 
     fos.write(intSent); 
     fos.write(numSentBytes); 
     fos.flush(); 
     fos.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 
} 

public static int getNumSentPackets() { 
    return nSentPackets; 
} 

预期的输出文件将是如下:

驱结果文件发送的数据包200

号码仅仅是一个例子,这就是我希望看到的有一个数字对应于发送的数据包总数。

预先感谢您。

+0

您能否在您的问题中添加完整的预期输出? – SirDarius 2014-10-31 09:38:32

+0

这就是我期待 驱结果文件 发送报文数 200是一个例子,但我想有numSentPackets – 2014-10-31 09:41:11

+0

我问你更新你的问题的价值,因为使用对此的评论丢弃所有换行符;) – SirDarius 2014-10-31 09:44:07

回答

0
ByteBuffer.allocate(capacity).putInt(yourInt).array(); 
+0

感谢您的回答,但可能您误读了。如果您阅读我的文章并查看我的代码,您会看到我正在使用该代码,但我没有得到我期望的结果。相反,我得到了一大堆我认为是十六进制的数字。 – 2014-10-31 09:39:44

1

因为我是一个懒惰的开发,我想利用现有的设施在我所选择的语言,例如,对于Java,一个PrintWriter

public void createFile(String filename) { 
    try { 
     File myFile = new File(Environment.getExternalStorageDirectory(), filename); 

     PrintWriter out = new PrintWriter(myFile); // this will create the file if necessary 

     out.println("FLOODING RESULTS FILE"); 
     out.println(); 
     out.print("Number of sent packets "); 
     out.println(mSender.getNumSentPackets()); 
     out.close(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 
} 

这比您目前的方法更容易阅读和维护,看起来更习惯。

+0

非常感谢!这可以解决!我对android和java完全陌生,并且我并不真正了解java提供的工具。谢谢!! – 2014-10-31 10:01:46

0

“200”的文本表示需要您写入3个字符。所有文件最后只是一堆字节,所以需要从字符映射到某个字节值。假设ASCII(*)的数据写入到该文件将是

//     '2','0','0' 
byte[] textVersion = { 50, 48, 48 } 

int另一方面是一个32位的数值,即,具有4个字节和200是等同于

byte[] intVersion = { 0, 0, 0, 200 } 

当使用一个ByteBuffer,你会得到这个。如果您将其写入文件并且文本查看器试图显示该文件,如果幸运的话,它会显示类似◻◻◻Č的内容。 A 0实际上是一个不可打印的控制字符,因此通常在打印时跳过或用像盒子一样的奇怪外观替换。 200将相当于Windows-CP1250中的Č。当解释为UTF8时它没有任何意义 - 它是2字节序列的开始,因此需要接下来的2个字节来确定要显示哪个字符。

您也可以使用

String.valueOf(200).getBytes(/* you should specify which encoding to use here */); 

将首先创建"200"字符串,然后返回你所需要的字节为那些3个字符。

但是,您应该使用基于Java的字符的IO设施:众多(并且令人困惑)的实现。它们全部(*^2)最后包装一个InputStreamOutputStream,并为您执行文本到字节转换。

PrintWriter可能是最方便的使用,但并非没有缺陷:https://stackoverflow.com/a/15803472/995891 FileWriter应该避免,因为你不能指定编码

较长的替代路线将

BufferedWriter writer = new BufferedWriter(
     new OutputStreamWriter(
      new FileOutputStream(file), encoding)); 
writer.write("Hello "); 
writer.write(String.valueOf(200)); 
writer.newLine(); 

(*)大部分编码对于基本上覆盖普通英文文本的前127个字符都是ASCII兼容的。

(*^2)没有任何东西强制Writer将字符输出到流中,例如, StringWriter。但他们大多用这种方式。