Java ASCII 输出到文件

发布于 2025-01-01 11:46:25 字数 1231 浏览 1 评论 0原文

当我尝试将 ASCII 值输出到文件时,对于某些字符,它会返回错误的值。例如:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;

    public class test {
    public static void main(String args[]){
                                  //Supposed to:
        writeFile("./test.txt"); //write ASCII 147
        readFile("./test.txt"); //read ASCII 147
    }

    public static boolean writeFile(String path){
        try{
            PrintWriter fo = new PrintWriter(new FileOutputStream(new File(path)));
            fo.print((char) 147); //WRITES "?" TO FILE (ASCII 63, NOT 147)
            fo.close();
        }catch(Exception e){
            return true;
        }
        return false;
    }

    public static boolean readFile(String path){
        try {
            BufferedReader fi = new BufferedReader(new FileReader(path));
            char c[] = fi.readLine().toCharArray();
            System.out.println((int) c[0]); //OBVIOUSLY PRINTS 63 INSTEAD OF 147
            fi.close();
            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}

我做错了什么?任何帮助都会很棒。谢谢。

When I try to output an ASCII value to a file, with some characters it returns the wrong value. Example:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;

    public class test {
    public static void main(String args[]){
                                  //Supposed to:
        writeFile("./test.txt"); //write ASCII 147
        readFile("./test.txt"); //read ASCII 147
    }

    public static boolean writeFile(String path){
        try{
            PrintWriter fo = new PrintWriter(new FileOutputStream(new File(path)));
            fo.print((char) 147); //WRITES "?" TO FILE (ASCII 63, NOT 147)
            fo.close();
        }catch(Exception e){
            return true;
        }
        return false;
    }

    public static boolean readFile(String path){
        try {
            BufferedReader fi = new BufferedReader(new FileReader(path));
            char c[] = fi.readLine().toCharArray();
            System.out.println((int) c[0]); //OBVIOUSLY PRINTS 63 INSTEAD OF 147
            fi.close();
            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}

What am I doing wrong? any help would be great. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

本宫微胖 2025-01-08 11:46:25

ASCII 中没有“字符 147”这样的东西。

您应该给出一个可以表示您感兴趣的字符的特定编码 - UTF-8 通常是一个不错的选择 - 并使用相同的编码进行读取和写入。

不幸的是 FileWriterFileReader 不允许您指定编码,因此您需要 FileOutputStreamFileInputStream包装在 OutputStreamWriterInputStreamReader 中:

import java.io.*;
import java.nio.charset.*;

public class Test {

    private static Charset UTF8 = Charset.forName("UTF-8");

    public static void main(String args[]) throws IOException {
        writeFile("./test.txt");
        readFile("./test.txt");
    }

    public static void writeFile(String path) throws IOException {
        Writer writer = new OutputStreamWriter(new FileOutputStream(path), UTF8);
        try {
            writer.write((char) 147);
        } finally {
            writer.close();
        }
    }

    public static void readFile(String path) throws IOException {
        Reader reader = new InputStreamReader(new FileInputStream(path), UTF8);
        try {
            int c = reader.read();
            System.out.println(c);
        } finally {
            reader.close();
        }
    }
}

There's no such thing as "character 147" in ASCII.

You should give a specific encoding which can represent the characters you're interested in - UTF-8 is usually a good choice - and use the same encoding for both reading and writing.

Unfortunately FileWriter and FileReader don't allow you to specify the encoding, so you'll need FileOutputStream and FileInputStream wrapped in an OutputStreamWriter and InputStreamReader:

import java.io.*;
import java.nio.charset.*;

public class Test {

    private static Charset UTF8 = Charset.forName("UTF-8");

    public static void main(String args[]) throws IOException {
        writeFile("./test.txt");
        readFile("./test.txt");
    }

    public static void writeFile(String path) throws IOException {
        Writer writer = new OutputStreamWriter(new FileOutputStream(path), UTF8);
        try {
            writer.write((char) 147);
        } finally {
            writer.close();
        }
    }

    public static void readFile(String path) throws IOException {
        Reader reader = new InputStreamReader(new FileInputStream(path), UTF8);
        try {
            int c = reader.read();
            System.out.println(c);
        } finally {
            reader.close();
        }
    }
}
我只土不豪 2025-01-08 11:46:25

某些语言将字符和八位字节类型混为一谈(C 和 C++ 是值得注意的例子。)Java char 类型隐式为 UTF-16,并且 ASCII 必须使用 byte 类型表示。请参阅此处进行比较。请参阅此处获取 Java 编码指南。

Some languages conflate the character and octet types (C and C++ being notable examples.) Java char types are implicitly UTF-16 and ASCII would have to be represented using the byte type. See here for a comparison. See here for a Java encoding guide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文