Actually I'm working on a class to read a text file that contains the following elements:
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;
what I need to do with these elements is to rearrange them in order to have all HDAUDIO devices with its values in the top of my list and USB devices in the bottom like this:
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;
I thought first store them in an Array List but I don't know how would be the better way to achieve this, please give a hand, thanks in advance
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); } } }