简体   繁体   English

无法写入文件

[英]not able to write onto a file

I have written this code to read from a table and write onto a file, but I am unable to write to a file. 我已编写此代码以从表中读取并写入文件中,但是无法写入文件。 The file gets created, but its empty. 文件已创建,但为空。 I don't have a mysql problem tried to insert comments and debug. 我没有试图插入注释和调试的mysql问题。 Code compiles fine, but has a problem in creating a file: 代码可以正常编译,但是在创建文件时存在问题:

import java.io.File;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Formatter;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class tabletocsv {
    public static void main(String[] args) {
        System.out.println("enter the table name");
        Scanner input = new Scanner(System.in);
        String table = input.next();
        System.out.println("enter number of columns");
        int columns = input.nextInt();
        createcsv(table, columns);
        System.out.print(" csv created");
        displaycsv(table, columns);
    }

    static void displaycsv(String table, int count) {
        Scanner file123;
        try {
            file123 = new Scanner(new File("/Users/Tannishk/Documents/csv/" + table + ".csv"));
            //System.out.printf("reading");
            while (file123.hasNext()) {
                System.out.println("going inside");
                for (int i = 1; i <= count; i++) {
                    String a = file123.next();
                    System.out.print(a + " ");
                }
                System.out.println();
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(tabletocsv.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    static void createcsv(String table, int count) {
        Formatter x;
        Connection connection;
        Statement st;
        try {
            x = new Formatter("/Users/Tannishk/Documents/csv/" + table + ".csv");
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "root", "password");
            st = connection.createStatement();
            ResultSet rs = st.executeQuery("select * from " + table + ";");
            while (rs.next()) {
                for (int i = 1; i <= count; i++) {
                    String a = rs.getString(i);
                    x.format("%s ", a);
                    // System.out.print(a);
                }
                x.format("\n");
            }
        } catch (Exception e) {

        } finally {

        }
    }
}

尝试刷新并关闭格式化程序

I think you have missed to flush the file. 我认为您错过了刷新文件的操作。

static void createcsv(String table,int count)
{
    Formatter x = null;
    Connection connection;
    Statement st;
    try{

        x = new Formatter("/Users/Tannishk/Documents/csv/"+table+".csv");
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","password");
        st = connection.createStatement();
        ResultSet rs= st.executeQuery("select * from "+table+";");
        while(rs.next())
        {
            for(int i=1;i<=count;i++)
            {
                String a = rs.getString(i);
                x.format("%s ",a);
                // System.out.print(a);
            }
            x.format("\n");
        }
    }
    catch(Exception e)
    {

    }
    finally
    {

        try {
            x.flush();
            x.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Try this ... 尝试这个 ...

You need to close the output stream that you're using to write to the file. 您需要关闭用于写入文件的输出流。 For example if you're using a FileWriter object named fout to write to the file, you'll have to do fout.close() to actually have anything written to the file as otherwise it would just be in memory. 例如,如果您正在使用名为fout的FileWriter对象写入文件,则必须执行fout.close()才能将任何内容实际写入文件,否则它将仅在内存中。

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

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