如何读取 .txt 文件并将其结构切换为 2D 列表

我对这些数据结构感到很困惑,我只想构建一个 2D 类型的结构来使用这些数据。


这是txt文件:


COSTS A B C D E F G H I J SUPPLY

Source1 6 7 10 16 5 8 15 15 6 8 175 

Source2 10 14 8 17 13 9 18 20 9 7 200 

Source3 9 4 8 12 10 10 8 5 9 10 225 

Source4 12 8 9 10 6 15 4 9 7 0 300 

Source5 6 9 17 7 6 13 6 7 6 0 250 

Source6 9 10 9 13 9 8 9 3 4 9 100 

Source7 16 18 7 14 5 6 10 5 4 5 150 

Source8 7 5 8 3 8 5 10 8 8 14 300 

Source9 8 10 9 6 4 9 17 7 5 8 100 

Source10 5 8 4 5 7 14 6 3 13 9 200 

DEMAND 150 250 110 275 175 350 300 180 90 120


这是我现在的代码:


public static void main(String[] args) {

    Object[][] table = new Object[13][];

    Scanner sc = new Scanner(System.in);

    //sc.useDelimiter("\\Z"); 


    while(sc.hasNextLine()) {

        String sc1 = sc.nextLine();

        ArrayList<String> arrayList = 

            new ArrayList<String>(Arrays.asList(sc1.split(" ")));


        System.out.println(arrayList);      

    }


    sc.close();

}


繁花如伊
浏览 131回答 2
2回答

阿波罗的战车

我猜您想将数据加载到table = new Object[13][];if so 这是您需要使用的代码;public static void main(String[] args) {&nbsp; &nbsp; Object[][] table = new Object[13][];&nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; int i = 0;&nbsp; &nbsp; while (sc.hasNextLine()) {&nbsp; &nbsp; &nbsp; &nbsp; String sc1 = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; table[i] = sc1.split(" ");&nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; }&nbsp; &nbsp; sc.close();&nbsp; &nbsp; System.out.println(Arrays.deepToString(table));}基本上我只是将表的每个索引设置为一个字符串数组,其中包含按空格分隔的字符串。

长风秋雁

显然可以使用数组来完成这类事情,但是没有简单的方法可以知道您可能正在读取的文件中包含多少数据行,除非您当然知道您将要读取的特定数据文件将始终包含相同数量的数据线。许多程序员将使用 ArrayList 或 List 接口对象来保存数据,如果仍然需要一个类型的数组,则该集合将转换为该类型的数组。填充二维数组:首先,您需要知道数据文件中实际包含多少有效的文件行数据。最简单的方法是进行两次读取。第一遍计算文件中有多少有效数据行,以便正确确定数组大小,然后第二遍填充二维数组。有一些简单的方法可以确定文件可能包含的行数,但没有简单的方法可以确定它们是否是有效的数据行(实际包含数据的行)。您的特定数据文件似乎包含三个特定部分:Header部分(文件的第一行);原始数据部分(文件正文);和需求部分(文件的最后一行)。将每个部分检索到单独的数组中可能是有益的,因此我在下面提供的代码演示了如何实现这一点。首先,您需要建立 3 个类成员变量,它们将具有整个类的全局范围:String[] headerData;&nbsp; // First data Line in file is the Header InformationObject[][] data;&nbsp; &nbsp; &nbsp; // The 2D Object Array to hold raw File Dataint[] demandData;&nbsp; &nbsp; &nbsp;// Last data line in file is the Demand Information现在将以下方法复制/粘贴到您的类中:private void readDataFile(String filePath) {&nbsp; &nbsp; Scanner sc;&nbsp; &nbsp; int maxColumns = 0;&nbsp; &nbsp; int validLines = 0;&nbsp; &nbsp; int endLine;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; sc = new Scanner(new File(filePath));&nbsp; &nbsp; &nbsp; &nbsp; String dataLine = "";&nbsp; &nbsp; &nbsp; &nbsp; // First Pass:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; while (sc.hasNextLine()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fileLine = sc.nextLine().trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* Skip Blank Lines or lines that start with&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a comment character (if any)... */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (fileLine.equals("") || fileLine.startsWith(";") || fileLine.startsWith("#")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validLines++;&nbsp; //This line must be a valid data line, so increment counter.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataLine = fileLine;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (validLines == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get Header Information.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headerData = dataLine.split("\\s+");&nbsp; // Grab the Columns Header Names&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxColumns = headerData.length;&nbsp; &nbsp; &nbsp; &nbsp;// Get the number of data columns&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; endLine = validLines;&nbsp; &nbsp; &nbsp; &nbsp; demandData = new int[endLine - 2];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Initialize the demandData array&nbsp; &nbsp; &nbsp; &nbsp; data = new Object[endLine - 2][maxColumns]; // Initialize the data array&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Make sure the last line actually contains data&nbsp; &nbsp; &nbsp; &nbsp; if (dataLine.equals("")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // No it doesn't so there is no DEMAND line.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("No DEMAND Data Available In File!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sc.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; /* Fill the demandData int array with Integer data...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;The dataLine variable at this point will hold&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;the last valid file data line which happend to&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;be the Demand Line&nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; String[] dl = dataLine.split("\\s+"); // split the data line on 1 or more whitespaces.&nbsp; &nbsp; &nbsp; &nbsp; // Don't want the word "DEMAND" so we start from 1.&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i < dl.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; demandData[i-1] = Integer.parseInt(dl[i]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Second Pass (fill the data aray):&nbsp; &nbsp; &nbsp; &nbsp; sc = new Scanner(new File("MyDataFile.txt"));&nbsp; &nbsp; &nbsp; &nbsp; validLines = 0;&nbsp; &nbsp; &nbsp; &nbsp; while (sc.hasNextLine()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fileLine = sc.nextLine().trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* Skip Blank Lines or lines that start with&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a comment character (if any)... */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (fileLine.equals("") || fileLine.startsWith(";") || fileLine.startsWith("#")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validLines++;&nbsp; //This line must be a valid data line, so increment counter.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (validLines == endLine) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (validLines == 1) { continue; }&nbsp; // Skip the header line&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dl = fileLine.split("\\s+");&nbsp; &nbsp; &nbsp; &nbsp; // split the data line on 1 or more whitespaces.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < dl.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[validLines-2][i] = dl[i]; //String Object - Sources Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[validLines-2][i] = Integer.parseInt(dl[i]); // Integer Object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sc.close();&nbsp; /* Close the reader.&nbsp; &nbsp; &nbsp; &nbsp; Make sure the number of data rows equals the number&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; of Demand values otherwise inform of mismatch. */&nbsp; &nbsp; &nbsp; &nbsp; if (data.length != demandData.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Warning: There is missing DEMAND information (data mismatch)!\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ "There is no available Demand data for one of the Sources.");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; catch (FileNotFoundException ex) {&nbsp; &nbsp; &nbsp; &nbsp; System.err.println(ex.getMessage());&nbsp; &nbsp; }}以下是您可以如何使用此方法:readDataFile("10by10.txt");if (data != null) {&nbsp; &nbsp; // Header Information:&nbsp; &nbsp; System.out.println("Header Column Names:\n" +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Arrays.toString(headerData) + "\n");&nbsp; &nbsp; // The Actual Data:&nbsp; &nbsp; System.out.println("File Data:");&nbsp; &nbsp; for (int i = 0; i < data.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.toString(data[i]));&nbsp; &nbsp; }&nbsp; &nbsp; // The Demand Information:&nbsp; &nbsp; System.out.println("\nDemand Values For Each Row:\n" +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Arrays.toString(demandData) + "\n");}如果您在此处提供示例文件数据,则控制台窗口将显示以下内容:Header Column Names:[COSTS, A, B, C, D, E, F, G, H, I, J, SUPPLY]File Data:[Source1, 6, 7, 10, 16, 5, 8, 15, 15, 6, 8, 175][Source2, 10, 14, 8, 17, 13, 9, 18, 20, 9, 7, 200][Source3, 9, 4, 8, 12, 10, 10, 8, 5, 9, 10, 225][Source4, 12, 8, 9, 10, 6, 15, 4, 9, 7, 0, 300][Source5, 6, 9, 17, 7, 6, 13, 6, 7, 6, 0, 250][Source6, 9, 10, 9, 13, 9, 8, 9, 3, 4, 9, 100][Source7, 16, 18, 7, 14, 5, 6, 10, 5, 4, 5, 150][Source8, 7, 5, 8, 3, 8, 5, 10, 8, 8, 14, 300][Source9, 8, 10, 9, 6, 4, 9, 17, 7, 5, 8, 100][Source10, 5, 8, 4, 5, 7, 14, 6, 3, 13, 9, 200]Demand Values For Each Data Row:[150, 250, 110, 275, 175, 350, 300, 180, 90, 120]填充二维列表界面:在这里,我们做一些不同的事情,你会看到它可以变得多么容易。这次我们的readDataFile()方法返回一个 String 类型的 2D List Interface 对象,我们可以轻松地操作该对象来获取 Header Column Names、Raw Data 和 Demand Data 值。这是方法:&nbsp;private List<List<String>> readDataFile(String filePath) {&nbsp; &nbsp; Scanner sc;&nbsp; &nbsp; List<List<String>> list = new ArrayList<>();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; sc = new Scanner(new File(filePath));&nbsp; &nbsp; &nbsp; &nbsp; while (sc.hasNextLine()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fileLine = sc.nextLine().trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* Skip Blank Lines or lines that start with&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a comment character (if any)... */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (fileLine.equals("") || fileLine.startsWith(";") || fileLine.startsWith("#")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* Valid data line we want...add it to the list.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Here we split the data line into a String array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;then we use the Arrays.asList() method to convert&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;it to a List then we add that List to our 2D List.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;All done in one line of code.&nbsp; */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(Arrays.asList(fileLine.split("\\s+")));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sc.close();&nbsp; &nbsp; &nbsp;// Close the reader.&nbsp; &nbsp; }&nbsp; &nbsp; catch (FileNotFoundException ex) {&nbsp; &nbsp; &nbsp; &nbsp; System.err.println(ex.getMessage());&nbsp; &nbsp; }&nbsp; &nbsp; return list;}并针对您的示例数据文件使用此方法:// Read the file data and place it all into a 2D ListList<List<String>> list = readDataFile("10by10.txt");// Get the Header Column Names from first ArrayListString[] headerData = list.get(0).toArray(new String[0]);&nbsp; &nbsp;// The 2D Object Array to hold raw File DataObject[][] data = new Object[list.size() - 2][headerData.length];&nbsp; &nbsp; &nbsp;&nbsp;for(int i = 1; i < list.size() - 1; i++) {&nbsp; &nbsp; for (int j = 0; j < list.get(i).size(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; if (j == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[i - 1][j] = list.get(i).get(j);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[i - 1][j] = Integer.parseInt(list.get(i).get(j));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}// Last data line in file is the DEMAND ValuesString[] tmp = list.get(list.size() - 1).toArray(new String[0]);int[] demandData = new int[tmp.length - 1];for (int i = 1; i < tmp.length; i++) {&nbsp; &nbsp; demandData[i - 1] = Integer.parseInt(tmp[i]);}if (data != null) {&nbsp; &nbsp; // Header Information:&nbsp; &nbsp; System.out.println("Header Column Names:\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + Arrays.toString(headerData) + "\n");&nbsp; &nbsp; // The Actual Data:&nbsp; &nbsp; System.out.println("File Data:");&nbsp; &nbsp; for (int i = 0; i < data.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.toString(data[i]));&nbsp; &nbsp; }&nbsp; &nbsp; // The Demand Information:&nbsp; &nbsp; System.out.println("\nDemand Values For Each Row:\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + Arrays.toString(demandData) + "\n");}控制台窗口的输出将与上面显示的相同。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java