java中的文件处理 [英] file handling in java

查看:141
本文介绍了java中的文件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打开文件rahul.txt所以它显示我不是以字节形式存储的字符,第二个问题是当我使用FileInputStream读取文件rahul.txt所以它没有读取。

package com.fileh;

import java .io。*;



公共类Writedata {

public static void main(String args [])

{

try {

FileOutputStream fout = new FileOutputStream(rahul.txt);

String s =我的名字是rahul;

byte [] b = s.getBytes();

fout.write(b) ;

fout.close();

}

catch(IOException ioe)

{

}

}

}

when i open file rahul.txt so it shows characters which i stored not in form of bytes and second problem is that when i use FileInputStream to read file rahul.txt so it did not read .
package com.fileh;
import java .io.*;

public class Writedata {
public static void main(String args[])
{
try{
FileOutputStream fout= new FileOutputStream("rahul.txt");
String s="my name is rahul";
byte[] b =s.getBytes();
fout.write(b);
fout.close();
}
catch(IOException ioe)
{
}
}
}

推荐答案

不,它以字节的形式保存。



可能你只是没有意识到这一点。您可能无法理解字节是什么以及它是如何呈现的。如果您认为byte会在某些文本编辑器读取的文件中看起来像0x2F,请再想一想。或者,如果需要,将每个字节格式化为字符串(2或4个字符,如本例所示)并输出结果字符串。现在,你可以在一些二进制编辑器中看到那些字节。



另外,你需要明白你的may name is rahul用Unicode表示, String.getBytes 的结果是使用平台的默认字符集的字节序列,将结果存储到新的字节arra:

< a href =http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#getBytes%28%29> http://docs.oracle.com/javase/7 /docs/api/java/lang/String.html#getBytes%28%29 [ ^ ]。



这不是很好由于这种不确定性。更确切地说,您需要指定精确的编码。有关详细信息,请参阅本教程: https://docs.oracle.com/javase/tutorial /i18n/text/string.html [ ^ ]。



例如,如果选择UTF-16,每个字符将获得两个字节。使用UTF-8,每个字符可以获得可变数量的字符串,并且可以涵盖完整的Unicode 字符集。使用ASCII,每个字符只能获得一个字节,但ASCII仅适用于低于128(小于或等于127)的代码点。在您的示例中,所有字符都适合ASCII范围,每个字符将获得1个字节,ASCII和UTF-8的字节相同,但ASCII不支持的其他字符,如果您使用它,可能会被替换为? ,这是不可接受的。所以,总是将UTF用于所有通用字符串。



-SA
No, it is saved in the form of bytes.

Probably you just did not realize it. It's possible that you don't understand what the byte is and how it is presented. If you think that the "byte" would look in the file read by some text editor, something as "0x2F", think again. Or, if you need that, format each byte as a string (2 or 4-character, as in this example) and output the resulting string. Right now, you can see those the bytes in some binary editor.

Also, you need to understand that your "may name is rahul" is represented in Unicode, and the result of String.getBytes is "a sequence of bytes using the platform's default charset, storing the result into a new byte arra":
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#getBytes%28%29[^].

This is not very good due to this uncertainty. To be more certain, you would need to specify exact encoding. For further detail, please see this tutorial: https://docs.oracle.com/javase/tutorial/i18n/text/string.html[^].

For example, if you choose UTF-16, you will get two bytes per characters. With UTF-8, you get variable number of strings per characters, and full Unicode character repertoire can be covered. With ASCII, you will get exactly one byte per character, but ASCII works only for code points below 128 (less or equal to 127). As in your example all characters fit in the ASCII range, you will get 1 byte per character, same bytes for ASCII and UTF-8, but other characters not supported by ASCII, in case you use it, may be replaced with '?', which is unacceptable. So, always use UTFs for all general-purpose strings.

—SA


这篇关于java中的文件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆