mirror of
https://github.com/processing/processing4.git
synced 2026-02-11 01:29:17 +01:00
simpler file i/o stuff for scripting
This commit is contained in:
@@ -1746,6 +1746,82 @@ public class PApplet extends Applet
|
||||
// FILE INPUT
|
||||
|
||||
|
||||
public File inputFile() {
|
||||
Frame frame = null;
|
||||
Component comp = getParent();
|
||||
while (comp != null) {
|
||||
if (comp instanceof Frame) {
|
||||
frame = (Frame) comp;
|
||||
break;
|
||||
}
|
||||
comp = comp.getParent();
|
||||
}
|
||||
//System.out.println("found frame " + frame);
|
||||
if (frame == null) frame = new Frame();
|
||||
|
||||
FileDialog fd = new FileDialog(frame,
|
||||
"Select a file...",
|
||||
FileDialog.LOAD);
|
||||
fd.show();
|
||||
|
||||
String directory = fd.getDirectory();
|
||||
String filename = fd.getFile();
|
||||
if (filename == null) return null;
|
||||
return new File(directory, filename);
|
||||
}
|
||||
|
||||
|
||||
public File outputFile() {
|
||||
Frame frame = null;
|
||||
Component comp = getParent();
|
||||
while (comp != null) {
|
||||
if (comp instanceof Frame) {
|
||||
frame = (Frame) comp;
|
||||
break;
|
||||
}
|
||||
comp = comp.getParent();
|
||||
}
|
||||
//System.out.println("found frame " + frame);
|
||||
if (frame == null) frame = new Frame();
|
||||
|
||||
FileDialog fd = new FileDialog(frame,
|
||||
"Save as...",
|
||||
FileDialog.SAVE);
|
||||
fd.show();
|
||||
|
||||
String directory = fd.getDirectory();
|
||||
String filename = fd.getFile();
|
||||
if (filename == null) return null;
|
||||
return new File(directory, filename);
|
||||
}
|
||||
|
||||
|
||||
public BufferedReader reader(File file) {
|
||||
try {
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
InputStreamReader isr = new InputStreamReader(fis);
|
||||
return new BufferedReader(isr);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public PrintWriter writer(File file) {
|
||||
try {
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
OutputStreamWriter osw = new OutputStreamWriter(fos);
|
||||
return new PrintWriter(osw);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public InputStream openStream(String filename) throws IOException {
|
||||
InputStream stream = null;
|
||||
|
||||
@@ -1825,6 +1901,17 @@ public class PApplet extends Applet
|
||||
}
|
||||
|
||||
|
||||
static public String[] loadStrings(File file) {
|
||||
try {
|
||||
return loadStrings(new FileInputStream(file));
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("problem loading strings from " + file);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String[] loadStrings(String filename) {
|
||||
try {
|
||||
return loadStrings(openStream(filename));
|
||||
|
||||
Reference in New Issue
Block a user