简体   繁体   English

如何获取给定字符串的文件大小?

[英]How do I get the file size of a given string?

If we had a string and we put that string into a text file... that text file would have a size.如果我们有一个字符串并将该字符串放入一个文本文件中……该文本文件将有一个大小。 Is there some sort of formula that we could use to calculate the size?有没有某种公式可以用来计算大小? I really need to do this because turning the string into a text file and getting the size then deleting the file is not working.我真的需要这样做,因为将字符串转换为文本文件并获取大小然后删除文件是行不通的。

Example using java.nio.Files :使用java.nio.Files示例:

public static long getStringSize(String str) throws IOException {
    Path file = Path.of("niofiles.txt");
    Files.writeString(file, str, StandardOpenOption.CREATE_NEW);
    long size = Files.size(file);
    Files.deleteIfExists(file); // cleanup
    return size;
}

Example using java.io.File :使用java.io.File示例:

public static long getStringSizeFile(String str) throws IOException {
    File file = new File("iofile.txt");
    try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
        bw.write(str);
    }
        
    long size = file.length();
    file.delete(); // cleanup
    return size;
}

Even simpler example to use length of the byte array:使用字节数组长度的更简单示例:

public static int stringSize(String str) {
    return str.getBytes().length;
}

All you need is to count bytes required to store you string, because one character could take more than one byte.您只需要计算存储字符串所需的bytes数,因为一个字符可能占用多个字节。

When you write n bytes to the file, this file is going to have exactly this amount of bytes.当您将n个字节写入文件时,该文件将恰好具有这个字节数。 Do use String.getBytes() to calculate it.请使用String.getBytes()来计算它。

public static void main(String... args) {
    String str = "endereço";
    System.out.println(str.length());                            // 8
    System.out.println(str.getBytes(StandardCharsets.UTF_8));    // 9
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得给定值是整数或字符串 - How do I get given value is Integer Or String 给定文件大小,为什么会出现java.lang.OutOfMemoryError? - Why do I get java.lang.OutOfMemoryError given the size of my file? 如何在android中获取临时文件的文件大小? - how do I get file size of temp file in android? 我怎么知道给定字符串的一部分是否是Arraylist中的字符串 <String> ? - How do I know if part of a given string is a string in a Arraylist<String>? 我如何获得给定阵列的频率? - How do i get the frequency of the given array? Java在Mac OSX上将别名(符号链接)报告为大小0。 如何获得真实的文件大小? - Java reports alias (symlink) as size 0 on Mac OSX. How do I get the true file size? 如果给定的字符串是给定的 firebase 位置的子项而不下载 firebase 中的整个数据快照,我如何获得 boolean 的值? - How do I get boolean value if given string is child of given firebase location without downloading whole datasnapshot in firebase? 如何规避字符串大小限制? - How do I circumvent the String size limit? 如何从文本文件中获取数据并将其保存为字符串? - how do I get data from a text file and save it into a string? 我怎么知道我的 GCM 消息是否足够小 (&lt; 4kb) 可以发送? (如何获取字符串的大小?) - How do i know if my GCM Message is small enough (< 4kb) to send? (How to get size of String?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM