By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,176 Members | 638 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,176 IT Pros & Developers. It's quick & easy.

Cannot read file...

JCEtajin
P: 6
somebody can check this code for me...i getting error...it said file not found

JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Select File To Open");
chooser.setCurrentDirectory(new File("."));

chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".enc")
|| f.isDirectory();
}

public String getDescription() {
return "Encryption File";
}
});

int r = chooser.showOpenDialog(new JFrame());
int arrlen = 10000;
byte[] infile = new byte[arrlen];
if (r == JFileChooser.APPROVE_OPTION) {
String name = chooser.getSelectedFile().getName();
System.out.println(name);
OpenBox.setText(String.valueOf(name));
//added code--------------------------------------
try {
FileInputStream fis = new FileInputStream(name);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
try {
int filelength = dis.read(infile);
String filestring = new String(infile, 0, filelength);
System.out.println("FILE CONTENT=" + filestring);
} catch(IOException iox) {
System.out.println("File read error...");
iox.printStackTrace();
}
} catch (FileNotFoundException fnf) {
System.out.println("File not found...");
fnf.printStackTrace();
}
}
Sep 27 '07 #1
Share this Question
Share on Google+
4 Replies

Nepomuk
Expert 2.5K+
P: 3,112
somebody can check this code for me...i getting error...it said file not found
Expand|Select|Wrap|Line Numbers
  1. JFileChooser chooser = new JFileChooser();
  2.   chooser.setDialogTitle("Select File To Open");
  3.     chooser.setCurrentDirectory(new File("."));
  4.  
  5.     chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
  6.       public boolean accept(File f) {
  7.         return f.getName().toLowerCase().endsWith(".enc")
  8.             || f.isDirectory();
  9.       }
  10.  
  11.       public String getDescription() {
  12.         return "Encryption File";
  13.       }
  14.     });
  15.  
  16.     int r = chooser.showOpenDialog(new JFrame());
  17.      int arrlen = 10000;
  18.      byte[] infile = new byte[arrlen];
  19.     if (r == JFileChooser.APPROVE_OPTION) {
  20.       String name = chooser.getSelectedFile().getName();
  21.       System.out.println(name);
  22.       OpenBox.setText(String.valueOf(name));
  23.     //added code--------------------------------------  
  24.            try {
  25.             FileInputStream fis = new FileInputStream(name); 
  26.             BufferedInputStream bis = new BufferedInputStream(fis);
  27.             DataInputStream dis = new DataInputStream(bis);
  28.             try {
  29.                 int filelength = dis.read(infile);
  30.                 String filestring = new String(infile, 0, filelength);
  31.                 System.out.println("FILE CONTENT=" + filestring);
  32.             } catch(IOException iox) {
  33.                 System.out.println("File read error...");
  34.                 iox.printStackTrace();
  35.             }
  36.         } catch (FileNotFoundException fnf) {
  37.             System.out.println("File not found...");
  38.             fnf.printStackTrace();
  39.         }
  40.                         }
I suspect, the Problem is in the Line FileInputStream fis = new FileInputStream(name);
Try this before:
Expand|Select|Wrap|Line Numbers
  1. File file = new File(name);
  2. System.out.println(file.getAbsolutePath() + " exists: " + file.exists());
  3.  
If that gives you "false", then the File doesn't exist. You can however check the full path of that file. Probably you made some mistake with the path.

Greetings,
Nepomuk

PS.: Please use CODE tags to post code.
Sep 27 '07 #2

Expert 10K+
P: 11,448
The JFileChooser gives you existing files only if you invoke the OpenDialog mode,
so that can't be the reason for the failure.

kind regards,

Jos
Sep 27 '07 #3

JCEtajin
P: 6
Expand|Select|Wrap|Line Numbers
  1. JFileChooser chooser = new JFileChooser();
  2.   chooser.setDialogTitle("Select File To Open");
  3.     chooser.setCurrentDirectory(new File("."));
  4.  
  5.     chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
  6.       public boolean accept(File f) {
  7.         return f.getName().toLowerCase().endsWith(".enc")
  8.             || f.isDirectory();
  9.       }
  10.  
  11.       public String getDescription() {
  12.         return "Encryption File";
  13.       }
  14.     });
  15.  
  16.     int r = chooser.showOpenDialog(new JFrame());
  17.      int arrlen = 10000;
  18.      byte[] infile = new byte[arrlen];
  19.     if (r == JFileChooser.APPROVE_OPTION) {
  20.       String name = chooser.getSelectedFile().getName();
  21.       System.out.println(name);
  22.       OpenBox.setText(String.valueOf(name));
  23.     //added code--------------------------------------  
  24.            try {
  25.             FileInputStream fis = new FileInputStream(name); 
  26.             BufferedInputStream bis = new BufferedInputStream(fis);
  27.             DataInputStream dis = new DataInputStream(bis);
  28.             try {
  29.                 int filelength = dis.read(infile);
  30.                 String filestring = new String(infile, 0, filelength);
  31.                 System.out.println("FILE CONTENT=" + filestring);
  32.             } catch(IOException iox) {
  33.                 System.out.println("File read error...");
  34.                 iox.printStackTrace();
  35.             }
  36.         } catch (FileNotFoundException fnf) {
  37.             System.out.println("File not found...");
  38.             fnf.printStackTrace();
  39.         }
  40.                         }
I suspect, the Problem is in the Line FileInputStream fis = new FileInputStream(name);
Try this before:
Expand|Select|Wrap|Line Numbers
  1. File file = new File(name);
  2. System.out.println(file.getAbsolutePath() + " exists: " + file.exists());
  3.  
If that gives you "false", then the File doesn't exist. You can however check the full path of that file. Probably you made some mistake with the path.

Greetings,
Nepomuk

PS.: Please use CODE tags to post code.
sorry..next i will code..i cannot make it right..can you explain more..that means i need to put that two line command..
Sep 27 '07 #4

JCEtajin
P: 6
sorry ..i still cannot get it..
Sep 27 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.