How to do things AI Noob vs. Pro
List biggest files Free Open Source: Swiss File Knifea command line
Depeche View
command line
free external tools,
cpp sources
articles |
Array Java programming: source code sample for simple Java arrays.How to read lines of a text file into a very simple fixed size array. Also shows how to sort the array content alphabetically, and how to write array content as another text file./* Java array source code example: - read text file into a simple array - with fixed maximum number of lines - sort the array - write array content into another text file. Put this text into a file tmp1.java. */ import java.io.*; public class tmp1 { // how many text lines can this program read? int MAX_TEXT_LINES = 1000; // up to 1000 lines // static start point (no object yet) public static void main(String args[]) throws Throwable { new tmp1().main2(args); } // dynamic start point (on a single object) public int main2(String args[]) throws Throwable { if (args.length < 2) return 9+perr("supply in- and output filename."); String sInFile = args[0]; String sOutFile = args[1]; // very simple static array for text storage: String aTextData[] = new String[MAX_TEXT_LINES+10]; // +10 are safety spaces, to avoid crash on off-by-one errors. int iTextSize = MAX_TEXT_LINES; // how large is the array int iTextUsed = 0; // how many lines contained // read input file into the array. BufferedReader fin = new BufferedReader( new InputStreamReader( new FileInputStream(args[0]), "ISO-8859-1" // or US-ASCII,UTF-8,UTF-16BE,UTF-16LE,UTF-16 )); while (true) { String sLine = fin.readLine(); if (sLine == null) break; // end of file // check if array has space for another line if (iTextUsed >= iTextSize) { perr("overflow: too many text lines\n"); break; } aTextData[iTextUsed] = sLine; iTextUsed++; } fin.close(); // do some processing on the array contents processData(aTextData, iTextUsed, iTextSize); // write the whole array to output file. PrintWriter fout = new PrintWriter( new OutputStreamWriter( new FileOutputStream(args[1]), "ISO-8859-1" )); for (int iLine=0; iLine<iTextUsed; iLine++) { fout.println(aTextData[iLine]); } fout.close(); return 0; } // processing example: sort array text lines alphabetically // with the simplest possible sorting algorithm (bubblesort). int processData(String aText[], int iLinesUsed, int iLinesAlloced) { int iOuterIdx, iInnerIdx; for (iOuterIdx=0; iOuterIdx<iLinesUsed; iOuterIdx++) { for (iInnerIdx=iOuterIdx+1; iInnerIdx<iLinesUsed; iInnerIdx++) { String sLine1 = aText[iOuterIdx]; String sLine2 = aText[iInnerIdx]; if (sLine1.compareTo(sLine2) > 0) { // swap both lines aText[iOuterIdx] = sLine2; aText[iInnerIdx] = sLine1; } } } print("sorted "+iLinesUsed+" lines.\n"); return 0; } // alternative processing example: sort using the Arrays class int processData2(String aText[], int iLinesUsed, int iLinesAlloced) { java.util.Arrays.sort(aText, 0, iLinesUsed); return 0; } // helper functions for better readability static int perr(String s) { System.err.print("error: "+s); return 0; } static int print(String s) { System.out.print(s); return 0; } };// See also: Java example for variable sized array . |