文件中的Java字母替换

文件中的Java字母替换,java,file,Java,File,到目前为止,我已经完成了这项工作,我的程序可以工作,例如转动数字123。。。变成像abc这样的字母 但我的问题是我不能让它与特殊字符一起工作,比如:č,ć,đ。问题是当我用特殊字符运行它时,我的文件只是被删除 编辑:忘了提到我正在使用.srt文件,在扫描仪中添加utf-8对txt文件有效,但当我尝试使用.srt时,它只是从文件中删除了完整内容 守则: LinkedList<String> lines = new LinkedList<String>(); // Open

到目前为止,我已经完成了这项工作,我的程序可以工作,例如转动数字
123
。。。变成像abc这样的字母

但我的问题是我不能让它与特殊字符一起工作,比如:
č,ć,đ
。问题是当我用特殊字符运行它时,我的文件只是被删除

编辑:忘了提到我正在使用
.srt
文件,在扫描仪中添加utf-8对txt文件有效,但当我尝试使用
.srt
时,它只是从文件中删除了完整内容

守则:

LinkedList<String> lines = new LinkedList<String>();

// Opening the file
Scanner input = new Scanner(new File("input.srt"), "UTF-8");
while (input.hasNextLine()) {
    String line = input.nextLine();
    lines.add(replaceLetters(line));
}
input.close();

// Saving the new edited version file
PrintWriter writer = new PrintWriter("input.srt", "UTF-8");
for (String line: lines) {
    writer.println(line);
}
writer.close();
LinkedList lines=新建LinkedList();
//打开文件
扫描仪输入=新扫描仪(新文件(“input.srt”),“UTF-8”);
while(input.hasNextLine()){
String line=input.nextLine();
行。添加(替换字母(行));
}
input.close();
//保存新编辑的版本文件
PrintWriter=新的PrintWriter(“input.srt”、“UTF-8”);
用于(字符串行:行){
writer.println(行);
}
writer.close();
替换方法:

public static String replaceLetters(String orig) {
    String fixed = "";
    // Go through each letter and replace with new letter
    for (int i = 0; i < orig.length(); i++) {
        // Get the letter
        String chr = orig.substring(i, i + 1);
        // Replace letter if nessesary
        if (chr.equals("a")) {
            chr = "1";
        } else if (chr.equals("b")) {
            chr = "2";
        } else if (chr.equals("c")) {
            chr = "3";
        }

        // Add the new letter to the end of fixed
        fixed += chr;
    }
    return fixed;
}
公共静态字符串替换字母(字符串源){
字符串固定=”;
//检查每个字母并替换为新字母
对于(int i=0;i
打开你的

Scanner input = new Scanner(new File("input.txt"));
进入

您保存在
UTF-8
中,但以默认字符集读取


另外,下次,请正确使用
try catch
语句,并将它们包含在您的帖子中。

请详细解释您试图实现的目标。请注意,现在您应该使用名为NIO的Java新I/O库来读写文件。它围绕
路径
文件
路径
展开。扩展答案:有了NIO,你就不会再有这样的问题了。默认情况下,如果不指定其他内容,它总是使用UTF-8<例如,代码>文件。写入(…)
文件。行(…)
Scanner input = new Scanner(new File("input.txt"), "UTF-8");