实际上我正在研究一个类来读取包含以下元素的文本文件:
Ringer-ID-0: USB\Device_Name_Here
Ringer-ID-0: {2344343r4efdwdd2w2sdwq}
Ringer-ID-0: volume =100;
Ringer-ID-0: speaker = off;
Ringer-ID-1: HDAUDIO\Device_Name_Here
Ringer-ID-1: {4r3444ewdwdaedw3re3d34d3}
Ringer-ID-1: volume = 93;
Ringer-ID-1: speaker = off;
Ringer-ID-2: HDAUDIO\Device_Name_Here
Ringer-ID-2: {23edecrrvt5y66h6hyyhy66}
Ringer-ID-2: volume = 93;
Ringer-ID-2: speaker = off;
Ringer-ID-3: HDAUDIO\Device_Name_Here
Ringer-ID-3: {3444444t56756y7h7h6ef}
Ringer-ID-3: volume = 93;
Ringer-ID-3: speaker = off;
Ringer-ID-4: USB\Device_Name_Here
Ringer-ID-4: {r4rfrf54g65g65g6h7uj7}
Ringer-ID-4: volume = 50;
Ringer-ID-4: speaker = on;
我需要对这些元素进行重新排列,以便让所有HDAUDIO设备的值都在我的列表顶部,而底部的USB设备就像这样:
Ringer-ID-0: HDAUDIO\Device_Name_Here
Ringer-ID-0: {4r3444ewdwdaedw3re3d34d3}
Ringer-ID-0: volume = 93;
Ringer-ID-0: speaker = off;
Ringer-ID-1: HDAUDIO\Device_Name_Here
Ringer-ID-1: {23edecrrvt5y66h6hyyhy66}
Ringer-ID-1: volume = 93;
Ringer-ID-1: speaker = off;
Ringer-ID-2: HDAUDIO\Device_Name_Here
Ringer-ID-2: {3444444t56756y7h7h6ef}
Ringer-ID-2: volume = 93;
Ringer-ID-2: speaker = off;
Ringer-ID-3: USB\Device_Name_Here
Ringer-ID-3: {2344343r4efdwdd2w2sdwq}
Ringer-ID-3: volume =100;
Ringer-ID-3: speaker = off;
Ringer-ID-4: USB\Device_Name_Here
Ringer-ID-4: {r4rfrf54g65g65g6h7uj7}
Ringer-ID-4: volume = 50;
Ringer-ID-4: speaker = on;
我认为首先将它们存储在数组列表中,但我不知道如何才能更好地实现这一目标,请提前帮助,先谢谢你们
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
public class RearrangeItems {
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new FileReader(new File("C:\\Users\\John\\Desktop\\items.txt")));
ArrayList<String> list = new ArrayList<String>();
String line = reader.readLine();
while (line != null) {
list.add(line);
line = reader.readLine();
}
reader.close();
String[] records = (String[]) list.toArray(new String[] { "" });
for (int i = 0; i < records.length; i++) {
System.out.println(records[i]);
}
} catch (Exception ex) {
System.out.println(ex);
}
}
}
答案 0 :(得分:0)
您可能会发现比较器接口可用于查看......
使用此方法,您可以使用扩展比较器的类,使用您希望的任何方法回调对可扩展的类进行排序。
http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/
我希望这会有所帮助......
答案 1 :(得分:0)
你可以这样做:
这里有一些工作代码来展示这个想法......
林格班: import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Ringer implements Comparable<Ringer> {
private static final String TYPE_USB = "USB";
private static final String TYPE_HDAUDIO = "HDAUDIO";
private static final String RINGER_NUMBER = "Ringer-ID-";
private String deviceType;
private String deviceName;
private String id;
private String volumeLevel;
private String speakerState;
public static Ringer parseAndCreate(String deviceLine, String idLine,
String volumeLine, String speakerLine) {
if (deviceLine == null || idLine == null || volumeLine == null
|| speakerLine == null) {
return null;
}
Ringer r = new Ringer();
String ringerNumber = null;
{ // device
Pattern pattern = Pattern.compile("^" + RINGER_NUMBER
+ "(\\d?):\\s?(.*)\\\\(.*)$");
Matcher matcher = pattern.matcher(deviceLine);
if (!matcher.find()) {
return null;
}
ringerNumber = matcher.group(1);
r.deviceType = matcher.group(2);
r.deviceName = matcher.group(3);
}
{ // id
Pattern pattern = Pattern.compile("^" + RINGER_NUMBER
+ "(\\d):\\s?\\{(.*)\\}$");
Matcher matcher = pattern.matcher(idLine);
if (!matcher.find() || !ringerNumber.equals(matcher.group(1))) {
return null;
}
r.id = matcher.group(2);
}
{ // volume
Pattern pattern = Pattern.compile("^" + RINGER_NUMBER
+ "(\\d):\\s?volume\\s?=\\s?(.*);$");
Matcher matcher = pattern.matcher(volumeLine);
if (!matcher.find() || !ringerNumber.equals(matcher.group(1))) {
return null;
}
r.volumeLevel = matcher.group(2);
}
{ // speaker
Pattern pattern = Pattern.compile("^" + RINGER_NUMBER
+ "(\\d):\\s?speaker\\s?=\\s?(.*);$");
Matcher matcher = pattern.matcher(speakerLine);
if (!matcher.find() || !ringerNumber.equals(matcher.group(1))) {
return null;
}
r.speakerState = matcher.group(2);
}
return r;
}
public String getFormated(int ringerNumber, String lineSeperator) {
StringBuilder s = new StringBuilder();
s.append(String.format("%s%d: %s\\%s%s", RINGER_NUMBER, ringerNumber,
deviceType, deviceName, lineSeperator));
s.append(String.format("%s%d: {%s}%s", RINGER_NUMBER, ringerNumber, id,
lineSeperator));
s.append(String.format("%s%d: volume = %s%s", RINGER_NUMBER,
ringerNumber, volumeLevel, lineSeperator));
s.append(String.format("%s%d: speaker = %s", RINGER_NUMBER,
ringerNumber, speakerState));
return s.toString();
}
@Override
public int compareTo(Ringer other) {
if (this.deviceType.equals(TYPE_HDAUDIO)
&& other.deviceType.equals(TYPE_USB)) {
return -1;
} else if (this.deviceType.equals(TYPE_USB)
&& other.deviceType.equals(TYPE_HDAUDIO)) {
return +1;
}
return 0;
}
@Override
public String toString() {
return "Ringer [deviceType=" + deviceType + ", deviceName="
+ deviceName + ", id=" + id + ", volumeLevel=" + volumeLevel
+ ", speakerState=" + speakerState + "]";
}
}
申请代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
public class RearrangeItems {
public static void main(String[] args) {
try {
ArrayList<Ringer> ringers = new ArrayList<Ringer>();
BufferedReader reader = new BufferedReader(new FileReader(new File(
"C:\\Users\\John\\Desktop\\items.txt")));
try {
while (true) {
String deviceLine = reader.readLine();
String idLine = reader.readLine();
String volumeLine = reader.readLine();
String speakerLine = reader.readLine();
Ringer ringer = Ringer.parseAndCreate(deviceLine, idLine,
volumeLine, speakerLine);
if (ringer == null) {
break;
}
ringers.add(ringer);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
reader.close();
}
Collections.sort(ringers);
for (int i = 0; i < ringers.size(); i++) {
System.out.println(ringers.get(i).getFormated(i, "\n"));
}
} catch (Exception ex) {
System.out.println(ex);
}
}
}