0

Someone gave me help on toggleing negative mode as per below, but I am very novice and I am wondering What import statements do I need for the folowing code to work in 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();
}
}

2 Answers2

1

You need:

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;

If you want, you can import all IO package, instead of having separate imports, like this: import java.io.*;

But in order for the code to actually work, you'll also need to fix some typos - a missing method name in OnCreate:

super.onCreate(savedInstanceState);

and a missing bracket to close the Try before the catch:

    }
}
catch (IOException e) {
SGill
  • 21
  • 5
0

Go to your imports, click inside it and then press:

CTRL+SHIFT+O

This should give you all imports you need.

Matthias Fischer
  • 473
  • 3
  • 15
silvia_aut
  • 1,435
  • 6
  • 19
  • 32