我需要编写一种方法,将文件中的数据读取到HashMap中,其键是文件行的编号,值是行中的文本。而且只有HashMap值(其键是2的幂)必须写入另一个文件。
我现在用一首诗创建了一个文件。编写了一种读取文件的方法,该文件在ArrayList中打印文本。运行循环并获取行号和文本会很方便。但是我不太明白下一步该怎么做。
public static void main(String[] args) throws IOException {
List<String> fileToRead = FileWriterReader.ourFileReaderToList("HAMLET.txt");
System.out.println(fileToRead);
public static List<String> ourFileReaderToList(String fileToRead) throws FileNotFoundException {
File file = new File(fileToRead);
List<String> poemLines = new ArrayList<String>();
Scanner in = null;
try {
if (file.exists()) {
in = new Scanner(file);
while (in.hasNextLine()) {
poemLines.add(in.nextLine());
}
}
} finally {
if (in != null) {
in.close();
}
}
return poemLines;
}
public static void ourWriterListToFile(String fileToWrite, List<String> listToWrite) throws IOException {
FileWriter fw = new FileWriter(fileToWrite, true);
BufferedWriter bw = null;
try {
bw = new BufferedWriter(fw);
int k = 0;
for (String s : listToWrite) {
if (k == (listToWrite.size() - 1)) {
bw.write(s);
} else {
bw.write(s + "\n");
}
k++;
}
} finally {
bw.close();
fw.close();
}
}
public static void parsedFile(List<String> listFromFile) {
for (String s : listFromFile)
System.out.println(s);
}
}
答案 0 :(得分:0)
您当然可以使用循环将ArrayList转换为HashMap。由于ourFileReaderToList
函数创建的ArrayList分别并按顺序存储每行,因此可以使用另一个函数从List中读取值并计数行号。
例如:
HashMap listToHashMap(List<String> list) {
HashMap<Integer, String> poemmap = new HashMap<Integer, String>(); //Our hashmap
int linenumber = 0; //Variable to keep track of the line number
for (String line : list) {
poemmap.put(linenumber, line); //Add line and line number to map
linenumber++; //Increment line number
}
return poemmap;
}
此函数将从列表中删除HashMap并将其返回。需要将行写入文件时,可以使用HashMap get()
函数检索这些行。
答案 1 :(得分:0)
您已使用现有的 ourFileReaderToList()方法完成了大部分任务,只需稍加修改即可容纳HashMap并按原样汇总文件行读入。方法的返回类型当然需要为 HashMap ,因为您希望 Key 为文件行号,并且每个键的值必须是当前正在读取的实际文件行字符串,您的“返回类型”必须为:public static void main(String[] args) {
List<Station> listOfStations = new ArrayList<>();
listOfStations.add(new Station("York", "American Railways"));
listOfStations.add(new Station("Moscow", "Russian Railways"));
listOfStations.add(new Station("Paris", "French Railways"));
Scanner scanner = new Scanner(System.in);
String getStationName = scanner.nextLine();
listOfStations.stream().filter((s)-> s.getNameOfTheStation().contains(getStationName)).forEach((s)-> System.out.println(s.toString()));
}
public class Station {
private String nameOfTheStation;
private String operatorOfTheStation;
public Station() {
}
public Station(String nameOfTheStation, String operatorOfTheStation) {
this.nameOfTheStation = nameOfTheStation;
this.operatorOfTheStation = operatorOfTheStation;
}
/**
* @return the nameOfTheStation
*/
public String getNameOfTheStation() {
return nameOfTheStation;
}
/**
* @param nameOfTheStation the nameOfTheStation to set
*/
public void setNameOfTheStation(String nameOfTheStation) {
this.nameOfTheStation = nameOfTheStation;
}
/**
* @return the operatorOfTheStation
*/
public String getOperatorOfTheStation() {
return operatorOfTheStation;
}
/**
* @param operatorOfTheStation the operatorOfTheStation to set
*/
public void setOperatorOfTheStation(String operatorOfTheStation) {
this.operatorOfTheStation = operatorOfTheStation;
}
@Override
public String toString() {
return String.format("%s operated by %s", getNameOfTheStation(), getOperatorOfTheStation());
}
}
:
HashMap<Integer, String>
由于不再使用方法中的 List 接口,因此可以继续将其更改为返回类型:
public static HashMap<Integer, String> ourFileReaderToList(String fileToRead) throws FileNotFoundException {
这是您的向HashMap归档文件方法的样子:
HashMap<Integer, String> poemLines = new HashMap<>();
现在您已将集合保存在HashMap中,则可以基于2的幂进行打印。我想这意味着先打印数据行2,然后打印数据行4,再打印行8,行16 ,32、64、128等,等等。。。直到 Powers Of 的值将大于集合中包含的文件行的可用数量(当然,如果您的意思是 2的幂)。您可以通过以下方式完成此操作:
public static HashMap<Integer, String> fileToHashMap(String fileToRead) throws FileNotFoundException {
Scanner reader = new Scanner(new File(fileToRead));
int lineCount = 0;
HashMap<Integer, String> poemLines = new HashMap<>();
try {
while (reader.hasNextLine()) {
lineCount++;
String fileLine = reader.nextLine();
// If you do not want to add blank lines to the
// collection then uncomment the following lines;
/*
if (fileLine.trim().equals("")) {
continue;
}
*/
poemLines.put(lineCount, fileLine); // Add to Collection
}
}
finally {
reader.close();
}
return poemLines;
}