簡體   English   中英

創建一個包含 10 列和 100 000 行的文件

[英]Create a file with 10 columns and 100 000 rows

private static final String SAMPLE_CSV_FILE_PATH = "./users.csv";

public static void main(String[] args) throws IOException {
    try (
        Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
        CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT);
    ) {
        for (CSVRecord csvRecord : csvParser) {
            // Accessing Values by Column Index
            String name = csvRecord.get(0);
            String email = csvRecord.get(1);
            String phone = csvRecord.get(2);
            String country = csvRecord.get(3);

            System.out.println("Record No - " + csvRecord.getRecordNumber());
            System.out.println("---------------");
            System.out.println("Name : " + name);
            System.out.println("Email : " + email);
            System.out.println("Phone : " + phone);
            System.out.println("Country : " + country);
            System.out.println("---------------\n\n");

這只是幾行和幾列的示例。 如何制作一個 10 列 10 萬行的文件以及如何僅通過編碼插入數據?

公共類文件生成器 {

public static void main(String[] args) throws Exception{
    File file = new File("D:\\SampleFiles\\test.txt");
    FileOutputStream fos = new FileOutputStream(file);
    String strName = "";
    String strDesi = "";
    String strCity = "";
    String strContac = "";
    fos.write("NAME\tDESIGNATION\tCITY\n".getBytes());
    for(int index=0;index<100000;index++){
        if(index < 10000){
            strName = "Farhana";
            strDesi = "Accountant";
            strCity = "Nagpur";
        }else if(index >= 10000 && index < 20000){
            strName = "Rahul";
            strDesi = "Manager";
            strCity = "Hyderabad";
        }
        else if(index >=20000 && index < 30000){
            strName = "Pavani";
            strDesi = "Architecture";
            strCity = "NewDelhi";

        }
        else if(index >= 30000 && index < 40000){
            strName = "Jay";
            strDesi = "Software Engineer";
            strCity = "Pune";

        }
        else if(index >= 40000 && index < 50000)
        {    
            strName = "Danish";
            strDesi = "Testing Engineer";
            strCity = "Mumbai";

        }
        else if(index >= 50000 && index < 60000){
            strName ="Lavani";
            strDesi = "Teacher";
            strCity = "London";
        }
        else if(index >=60000 && index < 70000){
            strName = "MoizAhmed";
            strDesi = "Customer service";
            strCity = "Newyork";
        }
        else if(index >=70000 && index < 80000){
            strName = "Azhar Khan";
            strDesi = "Front Manager";
            strCity = "Punjab";
        }
        else if(index >= 80000 && index < 90000){
            strName = "Rakesh kumar";
            strDesi = "Operation Analyst";
            strCity = "America";
        }
        else if(index >=90000 && index < 1000000){
            strName = "Zainab";
            strDesi = "Doctor";
            strCity = "Africa";

        }

        if(index == 99999){
            fos.write((strName + "\t" + strDesi + "\t" + strCity+ "\t" +strContac).getBytes());
            continue;
        }
        fos.write((strName + "\t" + strDesi + "\t" + strCity + "\t" +strContac+ "\n").getBytes());
    }
}

}

這是創建 pdf 文件的示例:

Document document = new Document();
    try {
        document.open();
        PdfWriter.getInstance(document, new FileOutputStream("iTextTable.pdf"));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    PdfPTable tablepositions = new PdfPTable(10);
    addPositionsTableHeader(tablepositions);
    public static void addPositionsTableHeader(PdfPTable table) {
    Stream.of("Column 1", "Column 2", "Column 3", "Column 4", "Column 5", "Column 6", "Column 7",
            "Column 8", "Column 9", "Column 10").forEach(columnTitle -> {
                PdfPCell header = new PdfPCell();
                header.setBackgroundColor(BaseColor.LIGHT_GRAY);
                header.setBorderWidth(1);
                header.setPhrase(new Phrase(columnTitle));
                table.addCell(header);
            });
}
public static void FillPositionsColumns(PdfPTable table, String string, int n) {
            for (i = 1; i <= n; i++) {
            table.addCell(string);
           }
}
FillPositionsColumns(tablepositions, "test", 10);
document.open();
document.add(tablepositions);
document.close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM