简体   繁体   中英

Read a text file from a URL line by line, print to a txt file and edit while being printed

This program read each line of the shirt size file available here: https://bbmedia.dmacc.edu/CIS/CIS171/shirtsizes.txt and write all the people who have order small shirts into a file called "smallshirts.txt", medium orders into a "mediumshirts.txt" file, large orders into a "largeshirts.txt" file and finally extra large shirts into "extralargeshirts.txt" file.

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Scanner;
      
public class SortShirtSizes {
    /**
     * This main method will get a file from the URL and separate it into different txt files
     */
    public static void main(String[] args) throws IOException {
    
        try {

        /*getting the txt from the URL*/

        String address = "https://bbmedia.dmacc.edu/CIS/CIS171/shirtsizes.txt";
        URL pageLocation = new URL(address);
        Scanner in = new Scanner(pageLocation.openStream());

         /*names of txt files*/

        PrintWriter outToSmall = new PrintWriter("smallshirts.txt");
        PrintWriter outToMed = new PrintWriter("mediumshirts.txt");
        PrintWriter outToLarge = new PrintWriter("largeshirts.txt");
        PrintWriter outToXl = new PrintWriter("extralargeshirts.txt");
         
        outToSmall.println("");
         
        String small = "";
         
            while (in.hasNextLine()) {
                small = in.nextLine();
                
                 /*print to txt files*/

                if (small.contains("S,")) {
                    outToSmall.println(small);
                }else if (small.contains("M,")) {
                    outToMed.println(small);
                }else if (small.contains("XL,")) {
                    outToXl.println(small);
                }else if (small.contains("L,")) {
                    outToLarge.println(small);
                }
            }
         
        in.close();
        outToSmall.close();
        outToMed.close();
        outToLarge.close();
        outToXl.close();
         
        }catch (IOException e) {
            System.out.println("https://bbmedia.dmacc.edu/CIS/CIS171/shirtorders2022.txt is not available.");
        }    
    }
}  

 

An example of the output:

 S,Darsie,Rigmand
 M,Feodor,Lornsen
 L,Klement,Antunes
 XL,Carleton,Van Leeuwen         

I want to remove the S, M, L, XL, and the , between the first and lastname.

There are a lot of ways to do this. But I am a fan of using libraries when there are some available. What you could do is use the Apache Commons CSV Library to read the Comma Separated File. Get the information and do whatever you need with it. To add the library, here is the Maven Repo link .

Example:

URL url = new URL("https://bbmedia.dmacc.edu/CIS/CIS171/shirtsizes.txt");

Reader fileReader = new InputStreamReader(url.openStream()); // Reading the Content of the online FIle

CSVFormat format = CSVFormat.Builder
        .create()
        .setDelimiter(",")
        .setHeader("size", "first_name", "last_name")
        .build(); // Setting the headers and delimiters for this specific format

CSVParser csvParser = new CSVParser(fileReader, format);

for(CSVRecord record : csvParser)
{
    String size = record.get("size");
    String firstName = record.get("first_name");
    String lastName = record.get("last_name");
    
    // Do whatever you want with the information above.

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM