有人给我提供了关于切换负面模式的帮助,如下所示,但我非常新手,我想知道我需要哪些导入语句才能使以下代码在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();
}
}
答案 0 :(得分: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);
以及在catch之前关闭 Try 的缺失括号:
}
}
catch (IOException e) {
答案 1 :(得分:0)
转到您的导入,在其中单击,然后按:
CTRL + SHIFT + O
这应该为您提供所需的所有进口。