无法写入文件

时间:2015-10-22 16:48:41

标签: java java-7 java-io

我已编写此代码以从表中读取并写入文件,但我无法写入文件。文件被创建,但它是空的。我没有尝试插入注释和调试的mysql问题。代码编译正常,但在创建文件时遇到问题:

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 {

        }
    }
}

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

我认为您错过了刷新文件。

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

试试这个......

答案 2 :(得分:0)

您需要关闭用于写入文件的输出流。例如,如果您使用名为 fout 的FileWriter对象来写入文件,则必须执行 fout.close()以实际写入任何内容。文件,否则它只会在内存中。