java - Android 中的 CSV 文件无法写入?

标签 java android io opencsv

我没有收到任何错误,但我不确定为什么此 CSV 实现不会写入文件...

代码:

    package com.tukajo.ioiograph;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.jjoe64.graphview.CustomLabelFormatter;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.LineGraphView;

import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;
import au.com.bytecode.opencsv.CSVWriter;

public class graph_fragment extends Fragment {

    Boolean csvThreadRunning;
    private customIOIO _myIOIO;
    globalControlInstance globalData = globalControlInstance.getInstance();
    private final Handler mHandler = new Handler();
    CSVWriter writer = null;
    private Runnable mTimer1;
    private Runnable mTimer2;
    private GraphView graphView;
    private int i = 0;
    ToggleButton realTimeToggle;
    TextView graphPeakText;
    TextView graphAvgText;
    TextView graphSamplesText;
    ToggleButton csvToggler;
    private double avgSumPin39 = 0.0;
    private double avgSumPin40 = 0.0;
    private double avgSumPin41 = 0.0;
    private double avgSumPin42 = 0.0;
    private double avgSumPin43 = 0.0;

    int graphLoopCounter = 0;

    private double newValuePin39 = 0;
    private double newValuePin40 = 0;
    private double newValuePin41 = 0;
    private double newValuePin42 = 0;
    private double newValuePin43 = 0;

    public static Fragment newInstance() {
        Fragment fragment = new graph_fragment();
        return fragment;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);

        _myIOIO = (customIOIO) getActivity().getApplicationContext();
        _myIOIO.create(getActivity().getBaseContext(), getActivity());
        // _myIOIO = MainActivity.get_myIOIO();
        _myIOIO.start();

        graphPeakText = (TextView) rootView.findViewById(R.id.graphpeak);
        graphAvgText = (TextView) rootView.findViewById(R.id.graphaverage);
        realTimeToggle = (ToggleButton) rootView
                .findViewById(R.id.realtimetoggle);

        realTimeToggle
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {

                        globalData.setLiveUpdatingEnabled(isChecked);
                        if (isChecked) {
                            updateGraphUI();
                            Log.v("PRESSED", "Button Pressed");
                        } else {
                            pauseGraphUI();
                            globalData.setGraphPeak1(0.0);
                            globalData.setGraphPeak2(0.0);
                            globalData.setGraphPeak3(0.0);
                            globalData.setGraphPeak4(0.0);
                            globalData.setGraphPeak5(0.0);
                        }

                    }
                });

        // set the initial text values for the graph peaks and the graph avgs
        graphPeakText.setText("Graph Peaks: " + globalData.getGraphPeak1()
                + " " + globalData.getGraphPeak2() + " "
                + globalData.getGraphPeak3() + " " + globalData.getGraphPeak4()
                + " " + globalData.getGraphPeak5());
        graphAvgText.setText("NOT IMPLEMENTED");

        GraphView graphView = new LineGraphView(getActivity(),
                "Graph with 5 lines");

        graphView.getGraphViewStyle().setNumHorizontalLabels(5);
        graphView.getGraphViewStyle().setNumVerticalLabels(8);
        graphView.addSeries(globalData.getGraphViewSeries1());
        graphView.addSeries(globalData.getGraphViewSeries2());
        graphView.addSeries(globalData.getGraphViewSeries3());
        graphView.addSeries(globalData.getGraphViewSeries4());
        graphView.addSeries(globalData.getGraphViewSeries5());
        graphView.setViewPort(0, 300);
        graphView.setScrollable(true);

        LinearLayout layoutForGraph = (LinearLayout) rootView
                .findViewById(R.id.graphLayout);

        layoutForGraph.addView(graphView);

        csvToggler = (ToggleButton) rootView.findViewById(R.id.ToggleButton2);
        csvToggler
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {

                        if (isChecked) {
                            try {
                                csvThreadRunning = true;
                                exportToCSV();
                            } catch (IOException e) {

                                e.printStackTrace();
                            }
                        } else {
                            csvThreadRunning = false;
                        }
                    }
                });
        return rootView;
    }

    public void exportToCSV() throws IOException {

        File file = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath()
                + File.separator
                + "csvData"
                + File.separator + "csvDataFile.txt");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        writer = new CSVWriter(new FileWriter(file), ',');
        new Thread() {
            public void run() {
                if (csvThreadRunning) {
                    while (!Thread.currentThread().isInterrupted()) {
                        try {

                            String s1 = String.valueOf(globalData
                                    .getLatestGraphData1()) + "pin39"; // array
                                                                        // of
                                                                        // your
                                                                        // values
                            writer.writeNext(s1);
                            s1 = String.valueOf(globalData
                                    .getLatestGraphData2()) + "pin40";
                            writer.writeNext(s1);
                            s1 = String.valueOf(globalData
                                    .getLatestGraphData3()) + "pin41";
                            writer.writeNext(s1);
                            s1 = String.valueOf(globalData
                                    .getLatestGraphData4()) + "pin42";
                            writer.writeNext(s1);
                            s1 = String.valueOf(globalData
                                    .getLatestGraphData5()) + "pin43";
                            writer.writeNext(s1);
                            writer.close();

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };

    }

    public void pauseGraphUI() {
        mHandler.removeCallbacks(mTimer1);
        mHandler.removeCallbacks(mTimer2);
    }

    public void updateGraphUI() {
        mTimer2 = new Runnable() {

            @Override
            public void run() {
                double newReadingDoublePin39 = 0.0;
                double newReadingDoublePin40 = 0.0;
                double newReadingDoublePin41 = 0.0;
                double newReadingDoublePin42 = 0.0;
                double newReadingDoublePin43 = 0.0;
                newReadingDoublePin39 = _myIOIO.getCurrentPin39Voltage();
                newReadingDoublePin40 = _myIOIO.getCurrentPin40Voltage();
                newReadingDoublePin41 = _myIOIO.getCurrentPin41Voltage();
                newReadingDoublePin42 = _myIOIO.getCurrentPin42Voltage();
                newReadingDoublePin43 = _myIOIO.getCurrentPin43Voltage();

                graphLoopCounter++;

                if (globalData.isLiveUpdatingEnabled()) {
                    globalData.setGraph2LastXValue(globalData
                            .getGraph2LastXValue() + 1d);
                    globalData.getGraphViewSeries1().appendData(
                            new TukajoData(globalData.getGraph2LastXValue(),
                                    newReadingDoublePin39), true, 1000);
                    globalData.getGraphViewSeries2().appendData(
                            new TukajoData(globalData.getGraph2LastXValue(),
                                    newReadingDoublePin40), true, 1000);
                    globalData.getGraphViewSeries3().appendData(
                            new TukajoData(globalData.getGraph2LastXValue(),
                                    newReadingDoublePin41), true, 1000);
                    globalData.getGraphViewSeries4().appendData(
                            new TukajoData(globalData.getGraph2LastXValue(),
                                    newReadingDoublePin42), true, 1000);
                    globalData.getGraphViewSeries5().appendData(
                            new TukajoData(globalData.getGraph2LastXValue(),
                                    newReadingDoublePin43), true, 1000);

                }

                mHandler.postDelayed(this, 50);
                Log.v("UPDATING", "Updating the graph");
                Log.v("newReadingValue", String.valueOf(newReadingDoublePin39));
            }
        };
        mHandler.postDelayed(mTimer2, 25);
    }

}

Logcat 没有产生任何我能看到的错误,并且我不确定我的 exportToCSV 方法没有执行其预期的操作有什么问题?

我在 exportToCSV 方法中调整了代码,如下所示:

 public void exportToCSV() throws IOException {

        File file1 = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath()
                + File.separator
                + "csvData"
                + File.separator);
        if (!file1.isDirectory()) {
            file1.mkdir();
        }

        File file = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath()
                + File.separator
                + "csvData"
                + File.separator + "csvDataFile.txt");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        writer = new CSVWriter(new FileWriter(file), ',');
        new Thread() {
            public void run() {
                if (csvThreadRunning) {
                    while (!Thread.currentThread().isInterrupted()) {
                        try {

                            String s1 = String.valueOf(globalData
                                    .getLatestGraphData1()) + "pin39"; // array
                                                                        // of
                                                                        // your
                                                                        // values
                            writer.writeNext(s1);
                            s1 = String.valueOf(globalData
                                    .getLatestGraphData2()) + "pin40";
                            writer.writeNext(s1);
                            s1 = String.valueOf(globalData
                                    .getLatestGraphData3()) + "pin41";
                            writer.writeNext(s1);
                            s1 = String.valueOf(globalData
                                    .getLatestGraphData4()) + "pin42";
                            writer.writeNext(s1);
                            s1 = String.valueOf(globalData
                                    .getLatestGraphData5()) + "pin43";
                            writer.writeNext(s1);
                            writer.close();

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }.start();

    }

现在我收到此错误:

java.io.FileNotFoundException ENOTDIR at the line

 writer = new CSVWriter(new FileWriter(file), ',');

最佳答案

您正在创建一个线程但未启动它。如果不启动它,它将永远不会被执行。

关于java - Android 中的 CSV 文件无法写入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25110829/

相关文章:

ruby - 在 Ruby 中打印可编辑到控制台

java - Mac OSX 上 IntelliJ Idea 中区分大小写的文件

java - hadoop 在所有 reducer 完成后给出输出

Android 按钮在 setText 后失去文本对齐

java - 有没有办法从操作系统访问(读/写)特定应用程序的 sqlite 数据库?

java - 如何对字符串数组进行排序和过滤?

Python:文件中的 Ascii 字符显示错误

Java PrintWriter 文件覆盖

java - Java 中的多集交集操作

JavaFX - 多维 HashMap 被覆盖