0

有人帮我按照下面的方式切换否定模式,但我很新手,我想知道我需要哪些导入语句才能使以下代码在 android 中工作:

public class ToggleNegativeColorsActivity extends Activity {

private static final String FILEPATH = "/sys/class/mdnie/mdnie/negative";

@Override
public void onCreate(Bundle savedInstanceState) {
    super(savedInstanceState);

    try {
        String value = readFileAsString(FILEPATH);
        if ("1".equals(value.trim())) {
            writeStringToFile(FILEPATH, "0");
        } else {
            writeStringToFile(FILEPATH, "1");
        }
    catch (IOException e) {
        e.printStackTrace();
    }

    finish();
}

// Grabbed from http://stackoverflow.com/questions/1656797/how-to-read-a-file-into-string-in-java
private String readFileAsString(String filePath) throws IOException {
    StringBuffer fileData = new StringBuffer();
    BufferedReader reader = new BufferedReader(
        new FileReader(filePath));
    char[] buf = new char[1024];
    int numRead;
    while((numRead=reader.read(buf)) != -1){
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
    }
    reader.close();
    return fileData.toString();
}

// Grabbed from http://stackoverflow.com/questions/1053467/how-do-i-save-a-string-to-a-text-file-using-java
private void writeStringToFile(String filePath, String value) throws IOException {
    PrintWriter out = new PrintWriter(filePath);
    out.print(value);
    out.close();
}
}
4

2 回答 2

1

你需要:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import android.os.Bundle;
import android.app.Activity;

如果需要,可以导入所有 IO 包,而不是单独导入,如下所示: import java.io.*;

但为了让代码真正起作用,您还需要修正一些拼写错误 - OnCreate 中缺少方法名称:

super.onCreate(savedInstanceState);

和一个缺少的括号来在捕获之前关闭Try :

    }
}
catch (IOException e) {
于 2013-10-31T15:00:09.293 回答
0

转到您的导入,在其中单击,然后按:

CTRL+SHIFT+O

这应该为您提供所需的所有导入。

于 2013-10-31T14:12:15.260 回答