Managing spaces when working with files

Hi, how can I work with files on windows on paths thats have blank spaces like "Documents and Settings", I used getResource().getFile() and writting the path but it always fails!

The FileOutputStream always thorws the exception FIleNotFoundException.

I tried the path with blank spaces and tried with %20 but always is the same.

[347 byte] By [MelGohana] at [2007-11-26 18:55:05]
# 1
You don't need to do anything.> The FileOutputStream always thorws the exception> FIleNotFoundException.Then you do something else incorrectly.
CeciNEstPasUnProgrammeura at 2007-7-9 20:32:52 > top of Java-index,Java Essentials,Java Programming...
# 2

Obviously I am doing somethig鏽g wrong and thats the reason for asking for help.

ok here is my code:

path=this.getClass().getResource("../").getFile()+"Configuracion/conexiones.omt";

salidaArchivos=new FileOutputStream (path);

or

path=this.getClass().getResource("../").getFile()+"Configuracion/conexiones.omt";

path=path.replace ("%20", " ");

salidaArchivos=new FileOutputStream (path);

both has the same problem.

But if I change the path for a String without spaces it works perfectly, please help.

MelGohana at 2007-7-9 20:32:52 > top of Java-index,Java Essentials,Java Programming...
# 3
You could in the very least call File's getName() instead of blindly concatenating whatever toString() returns. And I won't even try to find out what this getResource-stuff is supposed to do. Your code is highly confusing and looks brittle as well.
CeciNEstPasUnProgrammeura at 2007-7-9 20:32:52 > top of Java-index,Java Essentials,Java Programming...
# 4

This is the result of concatenating is:

C:/Documents and Settings/AAD/workspace/MobileServer/bin/Configuracion/conexiones.omt

and which part is confusing? There are 2 or 3 lines of codes on each test case.

I need to save the file on my app directory, how can I do it, anyone has a code for it?

MelGohana at 2007-7-9 20:32:52 > top of Java-index,Java Essentials,Java Programming...
# 5
The last time I checked the path separator on Windows was \ and not / as on Unix. It might normally work with / too but maybe in the presence of a space in the path name, who knows...
BIJ001a at 2007-7-9 20:32:52 > top of Java-index,Java Essentials,Java Programming...
# 6

Are you saying that OutputStream os = new FileOutputStream("C:/Documents and Settings/AAD/workspace/MobileServer/bin/Configuracion/conexiones.omt");

throws an exception? If so, what exception? If not then what are the symptoms of the problem?

sabre150a at 2007-7-9 20:32:52 > top of Java-index,Java Essentials,Java Programming...
# 7
Windows works with \ but java uses / or \\ to simulate it for obvious reassons like the \n \t and things like that. the / separator works perfectly when the path has no spaces so thats not the problem.
MelGohana at 2007-7-9 20:32:52 > top of Java-index,Java Essentials,Java Programming...
# 8

> Are you saying that > OutputStream os = new FileOutputStream("C:/Documents

> and

> Settings/AAD/workspace/MobileServer/bin/Configuracion/

> conexiones.omt");

> throws an exception? If so, what exception?

Exactly it throws an Exception.

java.io.FileNotFoundException: C:\Documents and Settings\AAD\workspace\MobileServer\bin\server\Configuracion\conexiones.omt (El sistema no puede hallar la ruta especificada)

at java.io.FileOutputStream.open(Native Method)

at java.io.FileOutputStream.<init>(FileOutputStream.java:179)

at java.io.FileOutputStream.<init>(FileOutputStream.java:70)

at server.tcp.GestionTCP.guardar(GestionTCP.java:118)

at server.tcp.GestionTCP.mouseReleased(GestionTCP.java:89)

at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:273)

at java.awt.Component.processMouseEvent(Component.java:6038)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)

at java.awt.Component.processEvent(Component.java:5803)

at java.awt.Container.processEvent(Container.java:2058)

at java.awt.Component.dispatchEventImpl(Component.java:4410)

at java.awt.Container.dispatchEventImpl(Container.java:2116)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)

at java.awt.Container.dispatchEventImpl(Container.java:2102)

at java.awt.Window.dispatchEventImpl(Window.java:2429)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

MelGohana at 2007-7-9 20:32:52 > top of Java-index,Java Essentials,Java Programming...
# 9
So the directory doesn't exist, I guess, and so you can't write a file into it. I don't understand Spanish, still it sounds to me like it's telling you exactly that.
CeciNEstPasUnProgrammeura at 2007-7-9 20:32:52 > top of Java-index,Java Essentials,Java Programming...
# 10

The your path to your file is wrong! See what results you get with with

File file = new File("C:/Documents and Settings/AAD/workspace/MobileServer/bin/Configuracion/conexiones.omt");

file.getParentFile().mkdirs();

OutputStream os = new FileOutputStream(file);

sabre150a at 2007-7-9 20:32:52 > top of Java-index,Java Essentials,Java Programming...
# 11
Well, this is a shame. I thought the FileOutputStream would create the directories if they dont exists but I was obviously wrong, thanks a lot!
MelGohana at 2007-7-9 20:32:52 > top of Java-index,Java Essentials,Java Programming...