Read the three columns in the TXT file to the matrix.

Source: Internet
Author: User
Import Java. io. bufferedreader; import Java. io. file; import Java. io. filereader; import Java. io. ioexception; import Java. util. *; import Java. io. bufferedwriter; import Java. io. *; // This program implements the function of reading three columns of numbers in a TXT file into a two-dimensional array. Public class Change {public static void main (string [] ARGs) {change a = New Change (); string filename = "D: \ Java \ workspace \ Java \ SRC \ input1.txt ";. readfilebylines (filename, filename + "-out.txt"); system. out. println ("finished... ");} public static void readfilebylines (string filename, string WF) {double [] [] matrix = new double [4] [4]; bufferedreader reader = NULL; // declare the buffer read object bufferedwriter writer = NULL; // Declare the buffer write object try {reader = new bufferedreader (New filereader (filename); writer = new bufferedwriter (New filewriter (WF); string tempstring = NULL; while (tempstring = reader. readline ())! = NULL) // read a row at a time until the end of reading null is {stringtokenizer ST = new stringtokenizer (tempstring); While (St. hasmoretokens () {int I, j = 0; Double A = 0.0; string Nt = ST. nexttoken (); I = integer. parseint (NT); // parse the abscissa if (St. hasmoretokens () {J = integer. parseint (St. nexttoken (); If (St. hasmoretokens () {A = double. parsedouble (St. nexttoken (); If (I <= 9 & J <= 3) {matrix [I] [J] = A;} else {system. out. println ("error") ;}}} reader. close (); For (INT I = 0; I <= 3; I ++) {for (Int J = 0; j <= 3; j ++) {double D = matrix [I] [J]; writer. write (D. tostring () + "\ t");} writer. write ("\ n");} writer. close ();} catch (ioexception e) {e. printstacktrace ();}}}

Learn how to read and write files
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.