读取 java 中的文件产生错误的字符

Reading file in java produces wrong characters

我目前正在学习 Coursera 生物信息学专业,但我被困在 反向补码问题 上。我不是在寻求这个问题的答案,因为这是不道德的。

当我使用测试数据集测试我的解决方案时,我将其作为字符串直接放入源代码中,我的答案是正确的。但是当我使用从文本文件中读取的数据集测试我的解决方案时,我得到了错误的答案。数据集由随机字符(A、T、C、G)组成。

我的主要方法是这样的:

public static void main(String[] args) throws IOException
{
    String dataset = readFile("filepath/dataset_3_2 (7).txt");
    String output = reverseComplement(dataset);
    BufferedWriter writer = null;
    try
    {
        writer = new BufferedWriter( new FileWriter("ergebnis.txt"));
        writer.write(output);

    }
    catch ( IOException e)
    {
    }
    finally
    {
        try
        {
            if ( writer != null)
            writer.close( );
        }
        catch ( IOException e)
        {
        }
    }
    System.out.println(checkForWrongCharacters(dataset));
    System.out.println("Invalid characters: " + returnOthers(dataset));
}

由于输入数据集应该只包含字母 A、G、C、T。因此我实现了两种方法来检查无效字符。

public static String returnOthers(String pattern)
{
    StringBuilder others = new StringBuilder();
    for(int i = 0; i < pattern.length(); i++)
    {
        char c = pattern.charAt(i);
        switch(c) {
        case 'A': continue;
        case 'G': continue;
        case 'T': continue;
        case 'C': continue;
        default: others.append(c);
        break;
        }
    }
    return others.toString();
}

这是另一个:

public static boolean checkForWrongCharacters(String pattern)
{
    boolean flag = false;
    StringBuilder result = new StringBuilder();
    for(int i = 0; i < pattern.length(); i++)
    {
        String s = "";
        char c = pattern.charAt(i);
        switch(c) {
        case 'A': continue;
        case 'G': continue;
        case 'T': continue;
        case 'C': continue;
        default: s = "Z";
        break;
        }
        result.append(s);
    }
    if(result.toString().contains("Z"))
    {
        flag = true;
    }
    else
    {
        flag = false;
    }
    return flag;
}

方法checkForWrongCharacters() return true,这意味着数据集中存在不是A,T,C或G的字符。但是方法returnOthers()没有return 随便什么。

有没有可能是我读取大文本文件时出现编码问题?

编辑

完全忘记了 post 我的 readFile() 方法...

public static String readFile(String filename) throws IOException
{
    String content = null;
    File file = new File(filename);
    FileReader reader = null;
    try {
         reader = new FileReader(file);
         char[] chars = new char[(int) file.length()];
         reader.read(chars);
         content = new String(chars);
         reader.close();
    } catch (IOException e) {
          e.printStackTrace();
    } finally {
        if(reader !=null){reader.close();}
    }
    return content;
}

这完成了工作。有回车 returns 和换行符弄乱了结果。

public static void main(String[] args) throws IOException
{
    String dataset = readFile("filepath/dataset_3_2 (7).txt");
    String dataset1 = dataset.replace("\r","");
    String dataset2 = dataset1.replace("\n","");
    String output = reverseComplement(dataset2);
    BufferedWriter writer = null;
    try
    {
        writer = new BufferedWriter( new FileWriter("ergebnis.txt"));
        writer.write(output);

    }
    catch ( IOException e)
    {
    }
    finally
    {
        try
        {
            if ( writer != null)
            writer.close( );
        }
        catch ( IOException e)
        {
        }
    }
    System.out.println(checkForWrongCharacters(dataset));
    System.out.println("Invalid characters: " + returnOthers(dataset));
}