473,569 Members | 2,383 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.setDial ogTitle("Select File To Open");
chooser.setCurr entDirectory(ne w File("."));

chooser.setFile Filter(new javax.swing.fil echooser.FileFi lter() {
public boolean accept(File f) {
return f.getName().toL owerCase().ends With(".enc")
|| f.isDirectory() ;
}

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

int r = chooser.showOpe nDialog(new JFrame());
int arrlen = 10000;
byte[] infile = new byte[arrlen];
if (r == JFileChooser.AP PROVE_OPTION) {
String name = chooser.getSele ctedFile().getN ame();
System.out.prin tln(name);
OpenBox.setText (String.valueOf (name));
//added code--------------------------------------
try {
FileInputStream fis = new FileInputStream (name);
BufferedInputSt ream bis = new BufferedInputSt ream(fis);
DataInputStream dis = new DataInputStream (bis);
try {
int filelength = dis.read(infile );
String filestring = new String(infile, 0, filelength);
System.out.prin tln("FILE CONTENT=" + filestring);
} catch(IOExcepti on iox) {
System.out.prin tln("File read error...");
iox.printStackT race();
}
} catch (FileNotFoundEx ception fnf) {
System.out.prin tln("File not found...");
fnf.printStackT race();
}
}
Sep 27 '07 #1
4 2242
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
10320
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 120''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
1
3944
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 update database or object is read-only". This is a workstation machine, not connected to any other computer, and I login to the PC using an...
2
1582
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 a file for reading, it throws an exception (e.g.: {"The process cannot access the file \"C:\\Documents and Settings\\Joerg\\Local...
8
5459
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 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I...
10
4427
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 Enterplise Linux 3 ES, and applied FixPack fp5_mi00069.tar to it. After creating an instance, starting the database, creating a database, and entering...
2
2586
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 in the xml file. It seems to be well formed and I was even able to read an earlier version of the file. This file is a result of being zipped. ...
3
3717
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 directory" Anybody have any idea why this file is locked? What do I have to do to release it? TIA ~jeff
15
2986
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 Tubbs
21
5794
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 'email address' text input but I am then having issues writing this data to a plain text file. Below is my html form followed by my perl script....
1
9054
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 still cannot use any FSO to read it. Here is my code: <% Dim fs set fs=Server.CreateObject("Scripting.FileSystemObject")
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8132
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
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.