繁体   English   中英

逐行从 URL 读取文本文件,打印到 txt 文件并在打印时进行编辑

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

该程序读取此处提供的衬衫尺寸文件的每一行: https ://bbmedia.dmacc.edu/CIS/CIS171/shirtsizes.txt 并将所有订购小衬衫的人写入名为“smallshirts.txt”的文件中,中型订单进入“mediumshirts.txt”文件,大订单进入“largeshirts.txt”文件,最后特大衬衫进入“extralargeshirts.txt”文件。

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.");
        }    
    }
}  

 

输出示例:

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

我想删除名字和姓氏之间的 S、M、L、XL 和 ,。

有很多方法可以做到这一点。 但我喜欢在有可用库时使用库。 您可以做的是使用Apache Commons CSV 库来读取逗号分隔文件。 获取信息并用它做任何你需要的事情。 要添加库,这里是Maven Repo 链接

例子:

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.

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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