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?
The java
Fileclass 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:There is another method of the file class called
getCanonicalPath()which may also be useful.