訪問字符串我有以下內容的文本文件:如何通過文件位置的Java
one
two
three
four
我想訪問字符串「三國」以其在Java.I文本文件中的位置在谷歌上找到了子串的概念,但無法使用它。
到目前爲止,我能夠讀取該文件的內容:
import java.io.*;
class FileRead
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("textfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
我想申請的子概念到file.It要求的位置並顯示出字符串。
String Str = new String("Welcome to Tutorialspoint.com");
System.out.println(Str.substring(10, 15));
格式化您的代碼。 – Shivam
請不要用Reader包裝DataInputStream。您不需要DataInputStream,因此將其刪除。 http://vanillajava.blogspot.co.uk/2012/08/java-memes-which-refuse-to-die.html#!/2012/08/java-memes-which-refuse-to-die.html –
你的問題是什麼? –