You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
2.6 KiB
106 lines
2.6 KiB
8 years ago
|
package sig;
|
||
|
import java.io.BufferedReader;
|
||
|
import java.io.File;
|
||
|
import java.io.FileReader;
|
||
|
import java.io.FileWriter;
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStream;
|
||
|
import java.io.InputStreamReader;
|
||
|
import java.io.PrintWriter;
|
||
|
import java.io.Reader;
|
||
|
import java.net.URL;
|
||
|
import java.nio.charset.Charset;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
|
||
|
import org.json.JSONException;
|
||
|
import org.json.JSONObject;
|
||
|
|
||
|
public class FileUtils {
|
||
|
public static String[] readFromFile(String filename) {
|
||
|
File file = new File(filename);
|
||
|
//System.out.println(file.getAbsolutePath());
|
||
|
List<String> contents= new ArrayList<String>();
|
||
|
if (file.exists()) {
|
||
|
try(
|
||
|
FileReader fw = new FileReader(filename);
|
||
|
BufferedReader bw = new BufferedReader(fw);)
|
||
|
{
|
||
|
String readline = bw.readLine();
|
||
|
do {
|
||
|
if (readline!=null) {
|
||
|
//System.out.println(readline);
|
||
|
contents.add(readline);
|
||
|
readline = bw.readLine();
|
||
|
}} while (readline!=null);
|
||
|
fw.close();
|
||
|
bw.close();
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
return contents.toArray(new String[contents.size()]);
|
||
|
}
|
||
|
|
||
|
private static String readAll(Reader rd) throws IOException {
|
||
|
StringBuilder sb = new StringBuilder();
|
||
|
int cp;
|
||
|
while ((cp = rd.read()) != -1) {
|
||
|
sb.append((char) cp);
|
||
|
}
|
||
|
return sb.toString();
|
||
|
}
|
||
|
|
||
|
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
|
||
|
InputStream is = new URL(url).openStream();
|
||
|
try {
|
||
|
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
|
||
|
String jsonText = readAll(rd);
|
||
|
JSONObject json = new JSONObject(jsonText);
|
||
|
return json;
|
||
|
} finally {
|
||
|
is.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void logToFile(String message, String filename) {
|
||
|
File file = new File(filename);
|
||
|
try {
|
||
|
|
||
|
if (!file.exists()) {
|
||
|
file.createNewFile();
|
||
|
}
|
||
|
|
||
|
FileWriter fw = new FileWriter(file, true);
|
||
|
PrintWriter pw = new PrintWriter(fw);
|
||
|
|
||
|
pw.println(message);
|
||
|
pw.flush();
|
||
|
pw.close();
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void writetoFile(String[] data, String filename) {
|
||
|
File file = new File(filename);
|
||
|
try {
|
||
|
|
||
|
if (!file.exists()) {
|
||
|
file.createNewFile();
|
||
|
}
|
||
|
|
||
|
FileWriter fw = new FileWriter(file,false);
|
||
|
PrintWriter pw = new PrintWriter(fw);
|
||
|
|
||
|
for (String s : data) {
|
||
|
pw.println(s);
|
||
|
}
|
||
|
pw.flush();
|
||
|
pw.close();
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
}
|