Java 计算文件的行数
我有一个关于Java的问题: 我正试图制作一个程序,通过文件和检查一些东西,但为此我需要一些东西来计算行数,有人知道一个好的方法来做到这一点吗 我以前从未处理过文件,所以我真的不知道什么。Java 计算文件的行数,java,lines,file-handling,Java,Lines,File Handling,我有一个关于Java的问题: 我正试图制作一个程序,通过文件和检查一些东西,但为此我需要一些东西来计算行数,有人知道一个好的方法来做到这一点吗 我以前从未处理过文件,所以我真的不知道什么。 另外,我没有代码要显示,因为我不知道怎么做。通过在互联网上搜索,你可以自己找到这个。 不过,我还是用了一个代码: public static int countLines(String filename) throws IOException { InputStream is = ne
另外,我没有代码要显示,因为我不知道怎么做。通过在互联网上搜索,你可以自己找到这个。 不过,我还是用了一个代码:
public static int countLines(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++count;
}
}
}
return (count == 0 && !empty) ? 1 : count;
}
finally {
is.close();
}
}
通过在互联网上搜索,你可以自己找到这个。 不过,我还是用了一个代码:
public static int countLines(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++count;
}
}
}
return (count == 0 && !empty) ? 1 : count;
}
finally {
is.close();
}
}
通过在互联网上搜索,你可以自己找到这个。 不过,我还是用了一个代码:
public static int countLines(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++count;
}
}
}
return (count == 0 && !empty) ? 1 : count;
}
finally {
is.close();
}
}
通过在互联网上搜索,你可以自己找到这个。 不过,我还是用了一个代码:
public static int countLines(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++count;
}
}
}
return (count == 0 && !empty) ? 1 : count;
}
finally {
is.close();
}
}
您可以试试这个,在lines变量中,您将得到行数
public String getFileStream(final String inputFile) {
int lines = 0;
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader(inputFile)));
while (s.hasNext()) {
lines++;
}
} catch (final IOException ex) {
ex.printStackTrace();
} finally {
if (s != null) {
s.close();
}
}
return result;
}
您可以试试这个,在lines变量中,您将得到行数
public String getFileStream(final String inputFile) {
int lines = 0;
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader(inputFile)));
while (s.hasNext()) {
lines++;
}
} catch (final IOException ex) {
ex.printStackTrace();
} finally {
if (s != null) {
s.close();
}
}
return result;
}
您可以试试这个,在lines变量中,您将得到行数
public String getFileStream(final String inputFile) {
int lines = 0;
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader(inputFile)));
while (s.hasNext()) {
lines++;
}
} catch (final IOException ex) {
ex.printStackTrace();
} finally {
if (s != null) {
s.close();
}
}
return result;
}
您可以试试这个,在lines变量中,您将得到行数
public String getFileStream(final String inputFile) {
int lines = 0;
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader(inputFile)));
while (s.hasNext()) {
lines++;
}
} catch (final IOException ex) {
ex.printStackTrace();
} finally {
if (s != null) {
s.close();
}
}
return result;
}
在Java 8中:
long lineCount = 0;
try (Stream<String> lines = Files.lines(Paths.get(filename))){
lineCount = lines.count();
} catch (final IOException i) {
// Handle exception.
}
long lineCount=0;
try(streamlines=Files.lines(path.get(filename))){
lineCount=lines.count();
}捕获(最终IOI){
//处理异常。
}
在Java 8中:
long lineCount = 0;
try (Stream<String> lines = Files.lines(Paths.get(filename))){
lineCount = lines.count();
} catch (final IOException i) {
// Handle exception.
}
long lineCount=0;
try(streamlines=Files.lines(path.get(filename))){
lineCount=lines.count();
}捕获(最终IOI){
//处理异常。
}
在Java 8中:
long lineCount = 0;
try (Stream<String> lines = Files.lines(Paths.get(filename))){
lineCount = lines.count();
} catch (final IOException i) {
// Handle exception.
}
long lineCount=0;
try(streamlines=Files.lines(path.get(filename))){
lineCount=lines.count();
}捕获(最终IOI){
//处理异常。
}
在Java 8中:
long lineCount = 0;
try (Stream<String> lines = Files.lines(Paths.get(filename))){
lineCount = lines.count();
} catch (final IOException i) {
// Handle exception.
}
long lineCount=0;
try(streamlines=Files.lines(path.get(filename))){
lineCount=lines.count();
}捕获(最终IOI){
//处理异常。
}
您只需执行以下操作:-
BufferedReader br = new BufferedReader(new FileReader(FILEPATH));
int lineCount = 0;
while(br.readLine() != null)
lineCount++;
System.out.println(lineCount);
BufferedReader
类提供了readLine()
逐行读取文本,因此可以使用它来获取行数。只需执行以下操作:-
BufferedReader br = new BufferedReader(new FileReader(FILEPATH));
int lineCount = 0;
while(br.readLine() != null)
lineCount++;
System.out.println(lineCount);
BufferedReader
类提供了readLine()
逐行读取文本,因此可以使用它来获取行数。只需执行以下操作:-
BufferedReader br = new BufferedReader(new FileReader(FILEPATH));
int lineCount = 0;
while(br.readLine() != null)
lineCount++;
System.out.println(lineCount);
BufferedReader
类提供了readLine()
逐行读取文本,因此可以使用它来获取行数。只需执行以下操作:-
BufferedReader br = new BufferedReader(new FileReader(FILEPATH));
int lineCount = 0;
while(br.readLine() != null)
lineCount++;
System.out.println(lineCount);
BufferedReader
类提供了readLine()
,它逐行读取文本,因此可以使用它来获取行数。您可以简单地执行以下操作:
public static void main(String[] args) {
int linecount = 0;
try {
// Open the file
FileInputStream fstream = new FileInputStream("d:\\data.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(
fstream));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
if (strLine.trim().length() > 0) { // check for blank line
linecount++;
} else {
continue;
}
}
System.out.println("Total no. of lines = " + linecount);
// Close the input stream
br.close();
} catch (Exception e) {
// TODO: handle exception
}
}
您可以简单地执行以下操作:
public static void main(String[] args) {
int linecount = 0;
try {
// Open the file
FileInputStream fstream = new FileInputStream("d:\\data.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(
fstream));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
if (strLine.trim().length() > 0) { // check for blank line
linecount++;
} else {
continue;
}
}
System.out.println("Total no. of lines = " + linecount);
// Close the input stream
br.close();
} catch (Exception e) {
// TODO: handle exception
}
}
您可以简单地执行以下操作:
public static void main(String[] args) {
int linecount = 0;
try {
// Open the file
FileInputStream fstream = new FileInputStream("d:\\data.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(
fstream));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
if (strLine.trim().length() > 0) { // check for blank line
linecount++;
} else {
continue;
}
}
System.out.println("Total no. of lines = " + linecount);
// Close the input stream
br.close();
} catch (Exception e) {
// TODO: handle exception
}
}
您可以简单地执行以下操作:
public static void main(String[] args) {
int linecount = 0;
try {
// Open the file
FileInputStream fstream = new FileInputStream("d:\\data.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(
fstream));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
if (strLine.trim().length() > 0) { // check for blank line
linecount++;
} else {
continue;
}
}
System.out.println("Total no. of lines = " + linecount);
// Close the input stream
br.close();
} catch (Exception e) {
// TODO: handle exception
}
}
哦,那真是太快了,谢谢你!这似乎很有效(在main方法中很快进行测试)。这是一种非常糟糕且复杂的行计数方法。当我编写Java1.2时,我会想象这样做,但即使这样,我还是会使用
BufferedReader
。这里唯一的优点是你不能用长线打断它(我说的是2GB左右),但这很少是一个问题。@Kayaman它仍然有效,所以我将使用它,我将测试这两种方法,但它们似乎都是正确的:D@Kayaman是的,我知道这个方法很旧,但我仍然使用它,不要问我为什么我更喜欢它,太快了,谢谢你!这似乎很有效(在main方法中很快进行测试)。这是一种非常糟糕且复杂的行计数方法。当我编写Java1.2时,我会想象这样做,但即使这样,我还是会使用BufferedReader
。这里唯一的优点是你不能用长线打断它(我说的是2GB左右),但这很少是一个问题。@Kayaman它仍然有效,所以我将使用它,我将测试这两种方法,但它们似乎都是正确的:D@Kayaman是的,我知道这个方法很旧,但我仍然使用它,不要问我为什么我更喜欢它,太快了,谢谢你!这似乎很有效(在main方法中很快进行测试)。这是一种非常糟糕且复杂的行计数方法。当我编写Java1.2时,我会想象这样做,但即使这样,我还是会使用BufferedReader
。这里唯一的优点是你不能用长线打断它(我说的是2GB左右),但这很少是一个问题。@Kayaman它仍然有效,所以我将使用它,我将测试这两种方法,但它们似乎都是正确的:D@Kayaman是的,我知道这个方法很旧,但我仍然使用它,不要问我为什么我更喜欢它,太快了,谢谢你!这似乎很有效(在main方法中很快进行测试)。这是一种非常糟糕且复杂的行计数方法。当我编写Java1.2时,我会想象这样做,但即使这样,我还是会使用BufferedReader
。这里唯一的优点是你不能用长线打断它(我说的是2GB左右),但这很少是一个问题。@Kayaman它仍然有效,所以我将使用它,我将测试这两种方法,但它们似乎都是正确的:D@Kayaman是的,我知道这个方法很旧,但我仍然使用它,不要问我为什么我认为你更喜欢Java 8,但我不想使用Java 8,因为它还不是经常使用的:DFiles.lines()
应该在try-with-resources语句中打开@Dogyman Java 8的应用非常迅速;你最好尽快习惯它@这是真的!修复了。谢谢,但我不想使用Java 8,因为它还没有经常使用:DFiles.lines()
应该在try-with-resources语句中打开@Dogyman Java 8的应用非常迅速;你最好尽快习惯它@这是真的!修复了。谢谢,但我不想使用Java 8,因为它还没有经常使用:DFiles.lines()
应该在try-with-resources语句中打开@Dogyman Java 8的应用非常迅速;你最好尽快习惯它@fge,那是ve