473,508 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot read file...

JCEtajin
6 New Member
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
4 2236
Nepomuk
3,112 Recognized Expert Specialist
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
JosAH
11,448 Recognized Expert MVP
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
6 New Member
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
6 New Member
sorry ..i still cannot get it..
Sep 27 '07 #5

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

Similar topics

6
10310
by: Christopher Brandsdal | last post by:
Hi! I get an error when I run my code Is there any other way to get te information from my form? Heres the error I get and the code beneath. Line 120 is market with ''''''''''''Line...
1
3932
by: Fie Fie Niles | last post by:
I have IIS installed on XP Professional workstation machine. I have an ASP page that open connection to an Access database, then when trying to update the database, it gave me the error "cannot...
2
1572
by: Joerg Battermann | last post by:
Hello there, I was wondering how I can check whether I am allowed to read a certain file or not? Some of e.g. XP's files are not readable, not even by the Administrator, and when trying to open...
8
5451
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
10
4420
by: Jean-David Beyer | last post by:
I have some programs running on Red Hat Linux 7.3 working with IBM DB2 V6.1 (with all the FixPacks) on my old machine. I have just installed IBM DB2 V8.1 on this (new) machine running Red Hat...
2
2579
by: Jill | last post by:
Hi I am using ms acces 2002 to read an xml file. I am using the DOM object in ms access - version xml 4 with service pack 2. here is the start of the code below. But I am having trouble reading...
3
3707
by: JeffM | last post by:
I have a .dat file on a remote server that seems to be locked. I have tried to FTP to it and delete it, rename it, copy over it and I get the error. "Could not copy temporary files to the output...
15
2978
by: waltbrad | last post by:
Hello. I'm studying the book "C++ Primer Plus" by Stephan Prata. In chapter 6 he gives an exercise that reads from a file. The list is thus: 4 Sam Stone 2000 Freida Flass 100500 Tammy...
21
5785
by: Mick1000 | last post by:
Hi all, I am new to perl and this forum. I am trying to setup a mailing list subscription functionality for customers to receive a periodic newsletter from me. My perl program grabs the html form...
1
9046
by: tanya.wang | last post by:
I want to access files from a remote server so I mapped it in my server under z:\ for \\myserver\web\mysite\images\ I added this UNC path to my IIS virtual directory and name it as "upload" but I...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7495
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.