Home > Default > File open in java

File open in java

October 11Hits:0
Advertisement
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.

Answers

All you could use to detect the correct type would be the extension which might or might not be accurate. We'll assume for a moment that the extensions will be accurate. What you need to look into running native code. Follow the bouncing link.
http://www.google.com/search?hl=en&q=Java+%2B+native+code&btnG=Google+Search
Hope this helps,
PS.
One more thing, please use code tags when you're posting code, it makes it much easier to read.
Message was edited by:
puckstopper31

Read other 2 answers

Tags:

Related Articles

Copyright (C) 2019 wisumpire.com, All Rights Reserved. webmaster#wisumpire.com 14 q. 0.789 s.