在java中读取文件会产生错误的字符
我目前正在做Coursera生物信息学专业,我被困在反向补体问题上。我不是在问这个问题的答案,因为这是不道德的 当我用一个测试数据集测试我的解决方案时,我把它作为字符串直接放在源代码中,我的答案是正确的。但当我用从文本文件读取的数据集测试我的解决方案时,我得到了错误的答案。数据集由随机字符(A、T、C、G)组成 我的主要方法如下所示:在java中读取文件会产生错误的字符,java,string,stringbuilder,readfile,Java,String,Stringbuilder,Readfile,我目前正在做Coursera生物信息学专业,我被困在反向补体问题上。我不是在问这个问题的答案,因为这是不道德的 当我用一个测试数据集测试我的解决方案时,我把它作为字符串直接放在源代码中,我的答案是正确的。但当我用从文本文件读取的数据集测试我的解决方案时,我得到了错误的答案。数据集由随机字符(A、T、C、G)组成 我的主要方法如下所示: public static void main(String[] args) throws IOException { String dataset =
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 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));
}
您的readFile方法是什么样子的?能否打印从readFile()返回的内容以进行调试?我无法打印字符串数据集的内容(其中包含使用readFile()方法读取的文件的内容),因为内容太大。该文件包含8925个字符。当我尝试打印时,没有打印任何内容。在调试模式下,我可以看到数据集包含读取的文本文件的字符串,但我无法检查整个字符串,因为它有8925个字符。我发现了错误。有一个回车符(\r)被放入字符串数据集中。这样,您将得到四个独立的数据副本。您至少应该为所有
replace()
结果重用相同的变量。但是,如果文件由行组成,那么首先使用readLine()
读取文件会简单得多。
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;
}
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));
}