Solved

FileOutputStream can't create file

Posted on 2011-02-27
10
782 Views
Last Modified: 2012-05-11
Hi,

I am using the attached code to create a file, but exception was thrown out:

"The system cannot find the path specified"

This is the file I wanted to create:

c:/crawl_data/2011-02-27/guansheng/1233445dfaf+guansheng+T¿+2011-02-27_10-42-05+1.html

I also try to create the directory "c:/crawl_data/2011-02-27/guansheng/" first, but I can't create the directory with the second code piece.

I also did some research that I need to create the file before I use FileOutputStream to write to it. Is it true? How can I create and write to a file on the fly?

Thanks



String base_dir="c:/crawl_data/";
		String dir=ts.substring(0, 10)+"/"+domain+"/";
		String filename=code+"+"+domain+"+"+prod+"+"+ts+"+"+order+".html";
		try {
			byte[] utf8 = content.getBytes("UTF-8");
			File afile = new File(base_dir+dir+filename);
			System.out.println(base_dir+dir+filename);
			if(!afile.exists()){
				if(afile.createNewFile()){
					FileOutputStream out = new FileOutputStream(afile);
					out.write(utf8);
					out.close();
				}
			}
			
		} catch (UnsupportedEncodingException e) {
			System.out.println("UTF-8¿¿¿¿:code="+code+ "\n"+e.getMessage());
			return false;
		} catch (FileNotFoundException e) {
			System.out.println("¿¿¿¿¿:code="+code+ "\n"+e.getMessage());
			return false;
		} catch (IOException e) {
			System.out.println("IO¿¿:code="+code + "\n"+e.getMessage());
			return false;
		}

Open in new window

String dir = "c:/crawl_data/2011-02-27/guansheng/";
if((new File(dir)).mkdir()){
   System.out.println("done");
}

Open in new window

0
Comment
Question by:wsyy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 34994240
I guess, that means that you don't have upper folders in existyence.
If you use mkdirs() instead of mkdir() it will create all upper folders which are ecessary simpulatenoisly
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34994245
However if you believe that upper path, say c:/crawl_data/2011-02-27/ should already be in existence, then
it is worth investigating more why such error appears before you use mkdirs() instead of mkdir()
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34994252
If your path to the folder where you want to place your output file exists, then you don't need to create file in a special operator- just
writing through FileoutputStream should be enough. If the folder itself does not exist - it will thow an exception, but if the folder exists - file
willbe created on the fly
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:wsyy
ID: 34994284
for_yan, the directories are not in existent beforehand.

I change my code as below:

if(!afile.exists()){
                        if(afile.mkdirs()){
                              FileOutputStream out = new FileOutputStream(afile);
                              out.write(utf8);
                              out.close();
                        }
                  }

Note please that I now use mkdirs(). This time an FileNotFoundException exception was thrown out, together with "Access denied" info.

Why these two can be thrown out together? which is the main reason?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 125 total points
ID: 34994308
I'm not sure you can have the afile.mkdirs() and point output stream to FileOutputStream(afile)

afile.mksdirs() - here afile should be directory

output stream should be directed to the file so you need to create file form the directory string + filename

so if  String f is the name of the fiolder, then f.mkdirs() will create the folder.
but File f1 shoulde be f1 = new File(f + Systsem.getProperty("file.separator') + n); where n - is the name of the actual file.

There may be also other reasons, but this is waht comes immediately when I looked at your code
 


0
 

Author Comment

by:wsyy
ID: 34994358
for_yan, you are right. I need to separate the directory from the file.

one last question: how can I specify the read-only permission to the newly created file? Is it something that can be done by Java?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34994362
0
 

Author Comment

by:wsyy
ID: 34994455
the example shows how to set permission to a folder, right? how can i set it on file level?
0
 

Author Comment

by:wsyy
ID: 34994463
in addition, when i changed my code as follow:

File afile = new File(dir);
                  if(!afile.exists()){
                        if(afile.mkdirs()){
                              File f = new File(dir+filename);
                              f.setReadOnly();
                              FileOutputStream out = new FileOutputStream(f);
                              out.write(utf8);
                              out.close();
                        }
                  }

I can still modify and save the file which is supposed to be read-only.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34994473

No, the API implies that in this case it should work in the same format for both File and Directory
http://download.oracle.com/javase/1.5.0/docs/api/java/io/File.html#setReadOnly%28%29

Still I guess how well it works  very much depends on the underlying OS.
Check, in you case and if it does not work, then on Unix system
you can use the system command chmod, through the call like Runtime.getRuntime().exec("chmod...") from your java code
Perhaps there is something similar on Windows also
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why my table column Id is not passed to java object? 4 46
tomcat administrtor 12 68
how to debug htl and js pages 8 58
How to configure empty element in XML Document parser? 15 45
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question