简体   繁体   English

Android studio,打开一个文件,连续写入然后关闭

[英]Android studio, open a file , continuously write and then close

I have code that is generating data every second and displaying onscreen.我有每秒生成数据并显示在屏幕上的代码。 This all works fine but I want to create a log file of all the data to analyze later.这一切正常,但我想创建所有数据的日志文件以供稍后分析。

I can open/write/close a file each time data is created but I am unsure of how much processing power this is using as it is continually opening and closing the file每次创建数据时我都可以打开/写入/关闭文件,但我不确定这使用了多少处理能力,因为它不断打开和关闭文件

  String data= reading1","+reading2+","+time +"/n";
        try {
            FileOutputStream out = openFileOutput("data.csv", Context.MODE_PRIVATE);
            out.write(data.getBytes());
            out.close();
        } catch (Exception e) {
            e.printStackTrace();

I would prefer to have the file open when the start button is clicked.我希望在单击开始按钮时打开文件。

if ( v.getId() == R.id.start ){
                // checks which button is clicked
                Log.d("dennis", "Scan working"); //logs the text
                // open a file
                try {
                    FileOutputStream out = openFileOutput("data.csv", Context.MODE_PRIVATE);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

but when it comes to closing the file, no options for .close() appear when out is typed但是在关闭文件时输入out不会出现 .close() 选项

if ( v.getId() == R.id.stop ){
                // checks which button is clicked

                out. // no valid options appear
                messageValue.setText(R.string.stopButtonText);// changes the hallo world text
                readNoRead=false;

            }

Does all the open/write/close need to be together or is it possible to是否所有的打开/写入/关闭都需要放在一起或者是否有可能

***open file***
-----
Cycle through all the data
-----
***Close file***

It is definitely possible to open, process and close a file all in one block without closing the file.绝对可以在一个块中打开、处理和关闭文件而不关闭文件。

Your out variable is not showing any method suggestions because it has not been defined in that block.您的out变量未显示任何方法建议,因为它尚未在该块中定义。 Change the line换线

FileOutputStream out = openFileOutput("data.csv", CONTEXT.MODE_PRIVATE);

to

out = openFileOutput("data.csv", CONTEXT.MODE_PRIVATE); 

and then add FileOutputStream out;然后将FileOutputStream out;添加FileOutputStream out; to a line above the first if statement (outside of the block).到第一个if语句上方的一行(块外)。

You may want to also look into 'try-catch-finally', or 'try with resources' as options for closing files in a try-catch block.您可能还想查看 'try-catch-finally' 或 'try with resources' 作为关闭 try-catch 块中文件的选项。

You should store a link to your FileOutputStream on top level in your class.您应该在类的顶层存储指向FileOutputStream的链接。

Example to your code:您的代码示例:

FileOutputStream out;

void clickStart() {
    if (v.getId() == R.id.start){
        // checks which button is clicked
        Log.d("dennis", "Scan working"); //logs the text
        // open a file
        try {
            out = openFileOutput("data.csv", Context.MODE_PRIVATE);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
}

void writeData() {
    String data= reading1+","+reading2+","+time +"/n";
    try {
        out.write(data.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

void clickStop() {
    if (v.getId() == R.id.stop) {
        try {
            out.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
        messageValue.setText(R.string.stopButtonText);// changes the hello world text
            readNoRead=false;
        }
}

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

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