创建一个具有10列和100000行的文件

时间:2018-08-16 10:08:03

标签: java

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万行的文件,以及如何仅通过编码插入数据?

2 个答案:

答案 0 :(得分:0)

公共类FileGenerator {

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());
    }
}

}

答案 1 :(得分:-1)

这是创建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();