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

huangapple go评论69阅读模式
英文:

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.

答案1

得分: 3

使用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;
}

使用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;
}

更简单的示例,使用字节数组的长度:

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

Example using 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:

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;
}

答案2

得分: 0

以下是翻译好的内容:

你只需要计算存储字符串所需的“字节”数量,因为一个字符可能会占用多个字节。

当你写入 n 个字节到文件时,该文件将恰好有这么多字节。请使用 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
}
英文:

All you need is to count bytes required to store you string, because one character could take more than one byte.

When you write n bytes to the file, this file is going to have exactly this amount of bytes. Do use String.getBytes() to calculate it.

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
}

huangapple
  • 本文由 发表于 2020年8月31日 20:42:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63671048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定