在java中使用数组对文本文件排序时丢弃的第一个元素
我有这段代码可以在java中使用数组对文本文件进行排序,但排序时它总是丢弃文本的第一行 这是我的密码:在java中使用数组对文本文件排序时丢弃的第一个元素,java,arrays,sorting,text-files,Java,Arrays,Sorting,Text Files,我有这段代码可以在java中使用数组对文本文件进行排序,但排序时它总是丢弃文本的第一行 这是我的密码: import java.io.*; public class Main { public static int count(String filename) throws IOException { InputStream is = new BufferedInputStream(new FileInputStream(filename)); try
import java.io.*;
public class Main {
public static int count(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
while ((readChars = is.read(c)) != -1) {
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++count;
}
}
}
return count;
} finally {
is.close();
}
}
public static String[] getContents(File aFile) throws IOException {
String[] words = new String[count(aFile.getName()) + 1];
BufferedReader input = new BufferedReader(new FileReader(aFile));
String line = null; //not declared within while loop
int i = 0;
while ((line = input.readLine()) != null) {
words[i] = line;
i++;
}
java.util.Arrays.sort(words);
for (int k = 0; k < words.length; k++) {
System.out.println(words[k]);
}
return words;
}
public static void main(String[] args) throws IOException {
File testFile = new File("try.txt");
getContents(testFile);
}
}
输出为:
Alexandra
Amanda
Barbara
Ezabile
Jane
Daisy
为了解决这个问题,我必须在文本文件的开头插入一个空行,有没有办法不这样做?我不知道出了什么问题?使用List和add方法读取文件内容。
然后使用Collections.sort对列表进行排序。使用List和add方法读取文件内容。
然后使用Collections.sort对列表进行排序。我在Mac电脑上编译了您的代码,它对我很有用。尝试在hexeditor中打开文件,查看文件开头是否有特殊字符。这可能会导致第一行的排序不正确。我在Mac上编译了您的代码,它对我很有用。尝试在hexeditor中打开文件,查看文件开头是否有特殊字符。这可能会导致第一行的排序不正确。文件开头可能有一个BOM字节顺序标记。根据定义,它们将被解释为零宽度非中断空间 所以如果你有
String textA = new String(new byte[] { (byte)0xef, (byte)0xbb, (byte) 0xbf, 65}, "UTF-8");
String textB = new String(new byte[] { 66}, "UTF-8");
System.err.println(textA + " < " + textB + " = " + (textA.compareTo(textB) < 0));
并使用列表或其他结构,这样您就不必读取文件两次。您可能在文件的开头有一个BOM字节顺序标记。根据定义,它们将被解释为零宽度非中断空间 所以如果你有
String textA = new String(new byte[] { (byte)0xef, (byte)0xbb, (byte) 0xbf, 65}, "UTF-8");
String textB = new String(new byte[] { 66}, "UTF-8");
System.err.println(textA + " < " + textB + " = " + (textA.compareTo(textB) < 0));
并使用列表或其他结构,这样您就不必读取文件两次。尝试更简单的方法,如:
public static String[] getContents(File aFile) throws IOException {
List<String> words = new ArrayList<String>();
BufferedReader input = new BufferedReader(new FileReader(aFile));
String line;
while ((line = input.readLine()) != null)
words.add(line);
Collections.sort(words);
return words.toArray(new String[words.size()]);
}
public static void main(String[] args) throws IOException {
File testFile = new File("try.txt");
String[] contents = getContents(testFile);
for (int k = 0; k < contents.length; k++) {
System.out.println(contents[k]);
}
}
请注意,您不必迭代文件来确定它有多少行,而是将这些行添加到ArrayList,最后将其转换为数组。尝试一些更简单的方法,如:
public static String[] getContents(File aFile) throws IOException {
List<String> words = new ArrayList<String>();
BufferedReader input = new BufferedReader(new FileReader(aFile));
String line;
while ((line = input.readLine()) != null)
words.add(line);
Collections.sort(words);
return words.toArray(new String[words.size()]);
}
public static void main(String[] args) throws IOException {
File testFile = new File("try.txt");
String[] contents = getContents(testFile);
for (int k = 0; k < contents.length; k++) {
System.out.println(contents[k]);
}
}
请注意,您不必迭代文件来确定它有多少行,而是将这些行添加到ArrayList中,最后将其转换为数组。在纯文本编辑器中打开文本文件,周围应该有一些字符?另外,列表更适合于消除countI在windows上运行代码的开销我不需要在文件中放置空行来正确排序所有行。谢谢,问题是我已将文本文件的编码设置为UTF-8,以读取英语或阿拉伯语。在纯文本编辑器中打开文本文件,周围应该有一些字符?另外,列表更适合于消除Count的开销我在windows上运行代码我不需要在文件中放置空行来正确排序所有行。谢谢,问题是我已将文本文件的编码设置为UTF-8以读取英语或阿拉伯语。谢谢,问题是我将文本文件的编码设置为UTF-8以读取英语或阿拉伯语。谢谢,问题是我将文本文件的编码设置为UTF-8以读取英语或阿拉伯语。谢谢,这要简单得多,而且我注意到错误是因为对我的文本文件使用UTF-8编码。谢谢,这要简单得多,而且我注意到错误是因为对我的文本文件使用UTF-8编码。