
字符串是可以转变成字节数组,然后统计一下字节数组的长度即可,参考如下代码:
Java语言中,中文字符所占的字节数取决于字符的编码方式,一般情况下,采用ISO8859-1编码方式时,一个中文字符与一个英文字符一样只占1个字节;采用GB2312或GBK编码方式时,一个中文字符占2个字节;而采用UTF-8编码方式时,一个中文字符会占3个字节。
public static void main(String []args) throws UnsupportedEncodingException {
运行结果:2
System.out.println("测试".getBytes("ISO8859-1").length)
运行结果:4
System.out.println("测试".getBytes("GB2312").length)
运行结果:4
System.out.println("测试".getBytes("GBK").length)
运行结果:6
System.out.println("测试".getBytes("UTF-8").length)
}
c# 获取字符串的字节数C#截取字符串字节数 代码如下:
public int bytelenght(string str)
{
byte[] bytestr = System.Text.Encoding.Unicode.GetBytes(str)
int j = 0
for (int i = 0i <bytestr.GetLength(0)i++)
{
if (i % 2 == 0)
{
j++
}
else
{
if (bytestr[i] >0)
{
j++
}
}
}
return j
}
谢谢采纳
c++ 怎样获取一串字符串的字符数,不是字节数定义一个字符数组,然后根据字符长度循环得到字符
比如char ch[20]
这个20可以用字符实际长度getlength()获取
然后再循环获取字符
ch[i]
c#求字符串的字节数其实用不着那么复杂计算的。注意:C#中string.Length只是计算字符串“字符”的个数,不计算字节;但是汉字两个字节+数字(英文字符)一个字节,才是6个,简单的代码如下: byte[] bytes = Encoding.Default.GetBytes("1243我")Default(根据自己究竟是汉字还是数字等,自动合理分配内存所需要的字节空间)
Console.WriteLine(bytes.Length)
C#如何获得存放字符串的字节数组?byte[] System.Text.Encoding.Default.GetBytes(string s)
如何计算一个Unicode字符串的字节数
你就是一个byte一个byte读取的吧?一个汉字两个byte(不含surrogate情况),文件中存两个byte,你从文件中读取2个byte,就是这样 查看原帖>>
java下字符串和字节数组如何转换?strRead = String.copyValueOf(strRead.toCharArray(), 0, byBuffer.length])2、字符串转换成字节数组 byte[] byBuffer = new byte[200]byBuffer= strInput.getBytes()注意:如果字符串里面含有中文,要特别注意,在android系统下,默认是UTF8编码,一个中文字符相当于3个字节,只有gb2312下一个中文相当于2字节。
import java.io.File
import java.io.RandomAccessFile
/**
* 2016年8月31日下午7:00:37
*
* @author 3306 TODO 计算字节数
*
*/
public class FileUtil {
public static void main(String[] args) {
String filePath = "d:/test.txt"// d盘必须存在test.txt文件
readEachLine(filePath)
}
/**
* 打印文件每一行的字节数
*
* @param filePath
* 文件路径
*/
private static void readEachLine(String filePath) {
try {
File file = new File(filePath)
if (file.exists()) {// 文件存在
RandomAccessFile accessFile = new RandomAccessFile(file, "r")// 只赋予读的权限
String line = ""
long lineIndex = 1
while (null != (line = accessFile.readLine())) {
System.out.println("line" + (lineIndex++) + ": " + line.getBytes().length)// 打印行号和字节数
}
accessFile.close()
}
} catch (Exception e) {
e.printStackTrace()
}
}
}
JAVA中读取文件内容的方法有很多,比如按字节读取文件内容,按字符读取文件内容,按行读取文件内容,随机读取文件内容等方法,本文就以上方法的具体实现给出代码,需要的可以直接复制使用public class ReadFromFile {
/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*/
public static void readFileByBytes(String fileName) {
File file = new File(fileName)
InputStream in = null
try {
System.out.println("以字节为单位读取文件内容,一次读一个字节:")
// 一次读一个字节
in = new FileInputStream(file)
int tempbyte
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte)
}
in.close()
} catch (IOException e) {
e.printStackTrace()
return
}
try {
System.out.println("以字节为单位读取文件内容,一次读多个字节:")
// 一次读多个字节
byte[] tempbytes = new byte[100]
int byteread = 0
in = new FileInputStream(fileName)
ReadFromFile.showAvailableBytes(in)
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread)
}
} catch (Exception e1) {
e1.printStackTrace()
} finally {
if (in != null) {
try {
in.close()
} catch (IOException e1) {
}
}
}
}
/**
* 以字符为单位读取文件,常用于读文本,数字等类型的文件
*/
public static void readFileByChars(String fileName) {
File file = new File(fileName)
Reader reader = null
try {
System.out.println("以字符为单位读取文件内容,一次读一个字节:")
// 一次读一个字符
reader = new InputStreamReader(new FileInputStream(file))
int tempchar
while ((tempchar = reader.read()) != -1) {
// 对于windows下,\r\n这两个字符在一起时,表示一个换行。
// 但如果这两个字符分开显示时,会换两次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar)
}
}
reader.close()
} catch (Exception e) {
e.printStackTrace()
}
try {
System.out.println("以字符为单位读取文件内容,一次读多个字节:")
// 一次读多个字符
char[] tempchars = new char[30]
int charread = 0
reader = new InputStreamReader(new FileInputStream(fileName))
// 读入多个字符到字符数组中,charread为一次读取字符数
while ((charread = reader.read(tempchars)) != -1) {
// 同样屏蔽掉\r不显示
if ((charread == tempchars.length)
&&(tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars)
} else {
for (int i = 0i <charreadi++) {
if (tempchars[i] == '\r') {
continue
} else {
System.out.print(tempchars[i])
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace()
} finally {
if (reader != null) {
try {
reader.close()
} catch (IOException e1) {
}
}
}
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static void readFileByLines(String fileName) {
File file = new File(fileName)
BufferedReader reader = null
try {
System.out.println("以行为单位读取文件内容,一次读一整行:")
reader = new BufferedReader(new FileReader(file))
String tempString = null
int line = 1
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString)
line++
}
reader.close()
} catch (IOException e) {
e.printStackTrace()
} finally {
if (reader != null) {
try {
reader.close()
} catch (IOException e1) {
}
}
}
}
/**
* 随机读取文件内容
*/
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null
try {
System.out.println("随机读取一段文件内容:")
// 打开一个随机访问文件流,按只读方式
randomFile = new RandomAccessFile(fileName, "r")
// 文件长度,字节数
long fileLength = randomFile.length()
// 读文件的起始位置
int beginIndex = (fileLength >4) ? 4 : 0
// 将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex)
byte[] bytes = new byte[10]
int byteread = 0
// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
// 将一次读取的字节数赋给byteread
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread)
}
} catch (IOException e) {
e.printStackTrace()
} finally {
if (randomFile != null) {
try {
randomFile.close()
} catch (IOException e1) {
}
}
}
}
/**
* 显示输入流中还剩的字节数
*/
private static void showAvailableBytes(InputStream in) {
try {
System.out.println("当前字节输入流中的字节数为:" + in.available())
} catch (IOException e) {
e.printStackTrace()
}
}
public static void main(String[] args) {
String fileName = "C:/temp/newTemp.txt"
ReadFromFile.readFileByBytes(fileName)
ReadFromFile.readFileByChars(fileName)
ReadFromFile.readFileByLines(fileName)
ReadFromFile.readFileByRandomAccess(fileName)
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)