Java写入.csv文件

时间:2015-02-19 19:27:58

标签: java csv

我正在尝试写入.csv文件,但我一直收到错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    void is an invalid type for the variable writeToFile
    Syntax error on token "(", ; expected
    Syntax error on token ")", ; expected

该错误与该行相关联:

void writeToFile(String Filename){

这是我的代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileWriter;

public class writeFileExample {
    public static void main(String[] args) {
        void writeToFile(String Filename){
            double steps=0;
            File file=new File(Filename);
            file.createNewFile();
            FileWriter writer=new FileWriter(file);
            try {
                //Integrate integrate=new Integrate();
                //for (steps=10;steps<1000000;steps=steps*10){
                    //double area_value=integrate.integrate_function(steps);
                    writer.write("Steps"+","+"Area");
                    //}
                //System.out.println(area_value);
                writer.flush();
                writer.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    }
}

我看不到任何语法错误。

考虑到Reimeus&#39;下面的评论我编辑了一下。我现在有:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileWriter;

void writeToFile(String Filename){
public class writeFileExample {
    public static void main(String[] args) {
            double steps=0;

我收到错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Filename cannot be resolved to a variable

任何帮助表示感谢。

2 个答案:

答案 0 :(得分:3)

Java不支持嵌套方法。将writeToFile移出main方法

答案 1 :(得分:0)

public class Test {
    public static void main(String[] args) {
    try {
        writeToFile("c:\\abc.csv");
    } catch (IOException e) {
    // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }
    static public void writeToFile(String Filename) throws IOException 
    {
      .
      .
      .
      .
    }
}