如何从数组中读取URL并将URL信息写入CSV

时间:2016-09-14 23:31:32

标签: java arrays csv

我正在尝试修改私有ArrayList<String> readWriteData(URL[] urls2) throws MalformedURLException, IOException {以读取我放入数组的URL。阅读后,我需要创建一个包含这些信息的CSV文件。我坚持如何从我用它们制作的数组中提取URL。另外,我是否正确地使用Printwriter从URL中打印信息?

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;


/**
 * @author Unknown
 */
public class DataManager {

    private String[] fileNames;
    private URL[] urls;
    private ArrayList<String> gameData;
    Scanner s;

    public DataManager() {
    }

    /**
     * Initializes the fields fileNames and path with the input parameters. 
     * Instantiates the field urls to be the same size as fileNames. 
     * Iterates over urls instantiating each url in the array invoking the constructor 
     * using path concatenated with the respective fileName from fileNames. 
     * The field gameData is then initialized using a helper method readWriteData.
     * @param fileNames - list of csv files
     * @param path - the base address for the csv files
     * @throws IOException 
     */
    public DataManager(String[] fileNames, String path) throws IOException {
        this.fileNames = fileNames; 
        this.urls = new URL[this.fileNames.length];
        for (int i = 0; i < this.fileNames.length; i++){
            String file = this.fileNames[i];
            String concatenate = path + file;
            URL url = new URL(concatenate);
            this.urls[i] = url;
        }
        System.out.println(Arrays.toString(this.urls));
        readWriteData(this.urls);
    }

    /**
     * Reads in the data from the web. Instantiates the field gameData, 
     * instantiates a scanner and a printwriter (with the same file name, using UTF-8 encoding.)
     * Gets a line from the scanner, adds that line of data to the ArrayList and writes the 
     * line of data to the local file. 
     * Streams must be flushed before creating a new ones or data can be lost while writing
     * Either close one stream before opening another or invoke the stream's flush() method
     * @param urls2 - the web address, contents to be read into an ArrayList
     * @return gameData An ArrayList holding game data in sets of three Strings
     * @throws IOException 
     * @throws MalformedURLException 
     */
    private ArrayList<String> readWriteData(URL[] urls2) throws MalformedURLException, IOException {
        String fileName = null;
        PrintWriter pWriter = new PrintWriter(fileName, "UTF-8");
        for (int i = 0; i < this.urls.length; i++) {    
            // Trying to loop through url array
            Scanner in = new Scanner(url.openStream());
            String inputLine;
            while (in.hasNextLine()) {
            }

            in.close();
        }
        return this.gameData;
    }

    /**
     * Displays the contents of each file stored in gameData on screen in table format
     */
    private void displayData() {
    }

    /**
     * Displays the contents  in gameData on screen in table format
     * @param gameData - an ArrayList containing csv data 9 to a line
     * @param fileName - the name of the file stored in gameData
     */
    public void displayData(ArrayList<String> gameData, String fileName) {
    }

    /**
     * Reads in local files, creates a local ArrayList to store the data, 
     * displays the data in table format by invoking the helper method displayData, 
     * and returns the data
     * @param f - name of the file to be read into the local ArrayList variable.
     * @return an ArrayList with the contents of the file.
     */
    public ArrayList<String> readFromFile(String f) {
        return null;
    }

    /**
     * Tests the DataManager.
     * @param args - not used
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        String[] fileNames = {"50.csv", "100.csv", "500.csv"}; 
        String path = "http://people.uncw.edu/tompkinsj/331/ch07/";
        DataManager foo = new DataManager(fileNames, path);     
    }
}

0 个答案:

没有答案
相关问题