Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2008 Ariadne Foundation. * * This file is part of Ariadne Harvester. * * Ariadne Harvester is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Ariadne Harvester is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Ariadne Harvester. If not, see <http://www.gnu.org/licenses/>. ******************************************************************************/ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class Main { public static String[] readN(int n, File file) { String[] buffer = new String[n]; String[] result = null; int i = 0; try { BufferedReader reader = new BufferedReader(new FileReader(file)); String line = reader.readLine(); while (line != null) { if (!line.equals("")) buffer[i++ % n] = line; // if(i >= n) i = 0; line = reader.readLine(); } } catch (IOException e) { // TODO: handle exception } // StringBuffer result = new StringBuffer(); // if(i >= n){ // for(int j = 0; j < n; j++){ // result.append(buffer[ (i + j) % n ]).append("\n"); // } // } // else{ // for(int j = 0; j < i; j++){ // result.append(buffer[j]).append("\n"); // } // } if (i >= n) { result = new String[n]; for (int j = 0; j < n; j++) { result[j] = buffer[(i + j) % n]; } } else { result = new String[i]; for (int j = 0; j < i; j++) { result[j] = buffer[j]; } } return result; } }