beginning work on sketch i/o

This commit is contained in:
benfry
2004-01-20 16:50:32 +00:00
parent faf898f060
commit 2fb58c5b96
3 changed files with 64 additions and 75 deletions
+31 -7
View File
@@ -358,7 +358,6 @@ public class PdeBase {
static public void copyFile(File afile, File bfile) throws IOException {
//try {
InputStream from = new BufferedInputStream(new FileInputStream(afile));
OutputStream to = new BufferedOutputStream(new FileOutputStream(bfile));
byte[] buffer = new byte[16 * 1024];
@@ -373,7 +372,7 @@ public class PdeBase {
to = null;
#ifdef JDK13
bfile.setLastModified(afile.lastModified()); // jdk13+ required
bfile.setLastModified(afile.lastModified()); // jdk13+ required
#endif
//} catch (IOException e) {
// e.printStackTrace();
@@ -381,21 +380,46 @@ public class PdeBase {
}
/**
* Grab the contents of a file as a string.
*/
static public String loadFile(File file) throws IOException {
// empty code file.. no worries, might be getting filled up later
if (file.length() == 0) return "";
InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
BufferedReader reader = new BufferedReader(isr);
StringBuffer buffer = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
buffer.append('\n');
}
reader.close();
return buffer.toString();
}
/**
* Spew the contents of a String object out to a file.
*/
static public void saveFile(String contents, File location)
throws IOException {
static public void saveFile(String str,
File file) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(contents.getBytes())));
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(location)));
ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());
InputStreamReader isr = new InputStreamReader(bis);
BufferedReader reader = new BufferedReader(isr);
FileWriter fw = new FileWriter(file);
PrintWriter writer = new PrintWriter(new BufferedWriter(fw));
String line = null;
while ((line = reader.readLine()) != null) {
writer.println(line);
}
writer.flush();
writer.close();
writer.close();
}
+6 -44
View File
@@ -27,13 +27,14 @@ import java.io.*;
public class PdeCode {
String name; // pretty name (no extension), not the full file name
String preprocName; // name of .java file after preproc
File file;
int flavor;
String program;
boolean modified;
//History history; // later
//PdeHistory history; // TODO add history information
String preprocName; // name of .java file after preproc
public PdeCode(String name, File file, int flavor) {
@@ -44,24 +45,9 @@ public class PdeCode {
public void load() throws IOException {
program = null;
if (file.length() != 0) {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(files[i])));
StringBuffer buffer = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
buffer.append('\n');
}
reader.close();
program = buffer.toString();
} else {
// empty code file.. no worries, might be getting filled up
program = "";
}
program = PdeBase.loadFile(file);
//program = null;
/*
} catch (IOException e) {
PdeBase.showWarning("Error loading file",
@@ -80,30 +66,6 @@ public class PdeCode {
// TODO re-enable history
//history.record(s, PdeHistory.SAVE);
//File file = new File(directory, filename);
//try {
//System.out.println("handleSave: results of getText");
//System.out.print(s);
ByteArrayInputStream bis = new ByteArrayInputStream(s.getBytes());
InputStreamReader isr = new InputStreamReader(bis);
BufferedReader reader = new BufferedReader(isr);
FileWriter fw = new FileWriter(file);
PrintWriter writer = new PrintWriter(new BufferedWriter(fw));
String line = null;
while ((line = reader.readLine()) != null) {
//System.out.println("w '" + line + "'");
writer.println(line);
}
writer.flush();
writer.close();
/*
sketchFile = file;
setSketchModified(false);
message("Done saving " + filename + ".");
*/
PdeBase.saveFile(program, file);
}
}
+27 -24
View File
@@ -506,18 +506,7 @@ public class PdeEditor extends JFrame
item = newJMenuItem("Export", 'E');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
editor.message("Exporting code...");
try {
if (sketch.export()) {
message("Done exporting.");
} else {
// error message will already be visible
}
} catch (Exception e) {
editor.message("Error during export.");
e.printStackTrace();
}
buttons.clear();
handleExport();
}
});
@@ -920,17 +909,6 @@ public class PdeEditor extends JFrame
}
/**
* Quit, but first ask user if it's ok. Also store preferences
* to disk just in case they want to quit. Final exit() happens
* in PdeEditor since it has the callback from PdeEditorStatus.
*/
public void handleQuit() {
// check to see if the person actually wants to quit
doQuit();
}
// ...................................................................
@@ -1468,7 +1446,32 @@ public class PdeEditor extends JFrame
}
public void doQuit() {
/**
* Handles calling the export() function on sketch, and
* queues all the gui status stuff that comes along with it.
*/
public void handleExport() {
editor.message("Exporting code...");
try {
if (sketch.export()) {
message("Done exporting.");
} else {
// error message will already be visible
}
} catch (Exception e) {
editor.message("Error during export.");
e.printStackTrace();
}
buttons.clear();
}
/**
* Quit, but first ask user if it's ok. Also store preferences
* to disk just in case they want to quit. Final exit() happens
* in PdeEditor since it has the callback from PdeEditorStatus.
*/
public void handleQuit() {
// stop isn't sufficient with external vm & quit
// instead use doClose() which will kill the external vm
//doStop();