Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

file open in java

807606Feb 15 2007 — edited Feb 15 2007
I have created a small application to upload any file and save it in a temporary directory.
Then display all the file names which are in that directory.here's my code ....

MultipartRequest mr = null;
try {
mr = new MultipartRequest(req, ".");
BufferedReader br = new BufferedReader(new FileReader(fileName = (String)mr.getFilesystemName("importFile")));

} catch(Exception e) { System.out.println("Error uploading from file '"+fileName+"': "+e); e.printStackTrace();
} finally { new File(fileName).delete(); }



String dir = "C:\\asta-timesheet\\temp\\";
Vector fileVector = new Vector();
boolean result = false;
DataInputStream dis;
FileInputStream fin;

try {
String fileContent = "";
Enumeration files = mr.getFileNames();

String name = (String)files.nextElement();
String filename = mr.getFilesystemName(name);
String type = mr.getContentType(name);
File f = mr.getFile(name);
FileReader fs = new FileReader(f);
BufferedReader in = new BufferedReader(fs);
String s, s2 = new String();
while((s = in.readLine())!= null) {
s2 += s + "\n";
}
fileContent = s2;


in.close();

File file = new File(dir, fileName);
// Create file if it does not exist
boolean success = file.createNewFile();
if (success) {
InputStream inn = new FileInputStream(f);
OutputStream out = new FileOutputStream(file);

// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = inn.read(buf)) > 0) {
out.write(buf, 0, len);
}
inn.close();
out.close();
} else {
// File already exists
req.setAttribute("fileStatus", "exists");
}

} catch(Exception e) { System.out.println("Error uploading from file '"+fileName+"': "+e); e.printStackTrace();}


File dirr = new File(dir);

String[] children = dirr.list();
if (children == null) {

} else {
for (int i=0; i<children.length; i++) {
String filename = children;

}
}

// The list of files can also be retrieved as File objects
File[] files = dirr.listFiles();

for (int i = 0; i < files.length; i++) {
File f = files[i];
}

req.setAttribute("fileVector", files);

return returnPage;


*************************************

I need help with opening up the listed files.i.e if its a word doc.i need it to be open on ms word.I have file types like excel,jpg,txt....Can someone pls explain or provide code for this.

thx in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 15 2007
Added on Feb 15 2007
1 comment
194 views