• Post Reply Bookmark Topic Watch Topic
  • New Topic

file location to read in eclipse and windows cmd  RSS feed

 
feras almasri
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I'm reading a file from eclipse it's working perfectly but when I try to run the code from windws cmd it's giving me a problem I'm using this


but I don't want to use

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
feras almasri wrote:but I don't want to use

Why not? The normal way to get the length of a file is to create a File object and use its length() method, which is exactly what you're doing.

You could also use FileInputStream and get its length via getChannel().size(); however, I've never used this technique, so I can't advise on any "gotchas" if you do.

Also: Are you sure you want bytes? This technique is normally only used with binary files (eg, images), so if your file contains text, you may well be doing things wrong.

My suggestion: Back up and tell us WHAT you're trying to do, not HOW you want to do it.

Winston
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never do this:
feras almasri wrote:


If an exception occurs, you'll never know that anything went wrong. At least print the stack trace of the exception:
 
feras almasri
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reding and writing RSA Key pairs so I need to generate text files and read them again so I want t a technique where I don't need to specify the path every time for the file
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
feras almasri wrote:I'm reding and writing RSA Key pairs so I need to generate text files and read them again so I want t a technique where I don't need to specify the path every time for the file



Not sure of the "don't need to specify the path every time" argument. It is a program after all -- just specify it once, and store it somewhere. And when you don't want to specify it, meaning use the last location, then have your code use the previous copy stored somewhere. Also, this is what methods are for, if you don't want to do repeat logic (or want some sort of default logic), put it in a method.

Or are you saying that you don't want to specify it at all?

Henry
 
feras almasri
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant that I want the application look for for file inside the directory.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
feras almasri wrote:I meant that I want the application look for for file inside the directory.


The reason it works in eclipse and not work in the command line ... is because the two setups are different. Basically, the applications are started with different current working directories. There isn't any searching (or other magic) going on with the eclipse code versus the command line code.

If you want the two executions to work the same, yet, you don't want to modify your code to do the searching, then you need to modify your start scripts to change to the correct working directory before starting the application. Start the windows command line version in the same directory as the eclipse version directory, instead of one directory lower.

Henry

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!