Solved

different names in linux and windows...

Posted on 2011-02-16
7
312 Views
Last Modified: 2012-05-11

       filelocation = (String)session.getAttribute("selectedfolder");
         new_filelocation = filelocation + "\\"  + "testing.txt";
            PrintWriter out1 = new PrintWriter(new FileOutputStream(new_filelocation));
                  out1.println("hi");
            
            file location is giving me the path something like
            C:/users/temp
            so new_location is
                        C:/users/temp/testing.txt

            this works well in windows i.e., a file with name testing.txt is created and i can see msg "hi"
            in that

            but in linux

            the file is created with file name temp\testing.txt
            but i can see "hi" msg in that...

            how to overcome this problem....
0
Comment
Question by:shragi
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 34910337
Use
File f = new File(filelocation, "testing.txt");
PrintWriter out1 = new PrintWriter(new FileOutputStream(f);

Open in new window

0
 

Author Comment

by:shragi
ID: 34910630
@CEHJ

its working but what could be reason....

does linux never supports string concatination....
0
 
LVL 4

Expert Comment

by:DangerousJeff
ID: 34910667
I believe the issue is the use of different slashes to denote directory's in the different OSs
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

Assisted Solution

by:rrz
rrz earned 100 total points
ID: 34910900
CEHJ and DangerousJeff are both right. CEHJ has shown you the easiest way because with that method java takes care of the file separator for you.  You could also use a static field of File object.
http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html#separator  
For example, for your code, we would use  
new_filelocation = filelocation + File.separator  + "testing.txt";
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34911359
>>does linux never supports string concatination....

You're using a platform-specific separator - no need
0
 
LVL 92

Expert Comment

by:objects
ID: 34911481
>             the file is created with file name temp\testing.txt

because the file separator in linux is a / (not a \), so it treats it as character in the filename
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34914657
:)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now