0

I was trying to make a code that will take input from a user and prints out the file path. I came across a interesting example. But I got stuck understanding a line.

//gets input from the user
BufferedReader input = new BufferedReader(newInputStreamReader(s.getInputStream()));
String request = input.readLine();
String path = new String(); 

int start = 0;
int end = 0;

for (int a = 0; a < request.length(); a++) {
    if (request.charAt(a) == ' ' && start != 0) {
        end = a;
        break;
    } 
    if (request.charAt(a) == ' ' && start == 0) {
        start = a;
    }
}

path = request.substring(start + 2, end); 

Why is a 2 is added at the end?

2
  • maybe it truncates the hard-drive letter ? - "C:" Commented Nov 19, 2011 at 16:51
  • 1
    Apparently they don't want any of the user input until two characters after the first whitespace if there's spaces in the input. Commented Nov 19, 2011 at 16:57

1 Answer 1

1

The java File class does most anything you need. What are you actually trying to do? What is the input? What kind of output are you looking for?


Per your comment, if you have a file in the working path called index.html, then you could:

File file = new File( "./index.html" );
System.out.println( file.getAbsolutePath() );

There is another method of the file class called getCanonicalPath() which may also be useful.

Sign up to request clarification or add additional context in comments.

3 Comments

what i'm trying to do lets say i client sends a get method for index.html file. i'm trying to view the absolute path of that file.
@user1052462, you would probably get a better response if you explain what you are trying to do in the first place. Then you likely would not have gotten voted down...
@Lucas..thanks for your advice and help..I'll keep it in mind next time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.