「java文本获取」java获取文本框
本篇文章给大家谈谈java文本获取,以及java获取文本框对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java读取整个文本文件?
- 2、java中怎么获取部分文本
- 3、java中怎么获得一个文本文件的行数
- 4、怎样用java代码获取txt文本的指定值
- 5、java如何获取文件信息?
- 6、Java 怎么获取文本域的值?
java读取整个文本文件?
可以通过BufferedReader 流的形式进行流缓存,之后通过readLine方法获取到缓存的内容。
BufferedReader bre = null;
try {
String file = "D:/test/test.txt";
bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流
while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环
{
System.out.println(str);//原样输出读到的内容
};
备注: 流用完之后必须close掉,如上面的就应该是:bre.close(),否则bre流会一直存在,直到程序运行结束。
java中怎么获取部分文本
先获取全部内容然后再截取。
有两种方法。
String content = "我的名字是,路人甲 ";
content.split(",")[0];
content.subString(0,content.lastIndexOf(","));
上面这两种方法都可以。
java中怎么获得一个文本文件的行数
涉及到java中读写文件的IO操作。
获取一个文本文件的行数较为方便的方法,是通过BufferedReader类的readLine()方法,间接的统计行数。
源代码:
public
static
int
getTextLines()
throws
IOException
{
String
path
=
"c:\\job.txt"
;//
定义文件路径
FileReader
fr
=
new
FileReader(path);
//这里定义一个字符流的输入流的节点流,用于读取文件(一个字符一个字符的读取)
BufferedReader
br
=
new
BufferedReader(fr);
//
在定义好的流基础上套接一个处理流,用于更加效率的读取文件(一行一行的读取)
int
x
=
0;
//
用于统计行数,从0开始
while(br.readLine()
!=
null)
{
//
readLine()方法是按行读的,返回值是这行的内容
x++;
//
每读一行,则变量x累加1
}
return
x;
//返回总的行数
}
怎样用java代码获取txt文本的指定值
[Java] view plain copy
import java.io.*;
public class hh {
/**
* @param args
*/
public static void main(String[] args) {
// 指定读取的行号
int lineNumber = 2;
// 读取文件
//File sourceFile = new File("D:/java/test.txt");
File sourceFile = new File("C://TEXT.txt");
try {
// 读取指定的行
readAppointedLineNumber(sourceFile, lineNumber);
// 获取文件的内容的总行数
System.out.println(getTotalLines(sourceFile));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 读取文件指定行。
static void readAppointedLineNumber(File sourceFile, int lineNumber)
throws IOException {
FileReader in = new FileReader(sourceFile);
LineNumberReader reader = new LineNumberReader(in);
String s = "";
if (lineNumber = 0 || lineNumber getTotalLines(sourceFile)) {
System.out.println("不在文件的行数范围(1至总行数)之内。");
System.exit(0);
}
int lines = 0;
while (s != null) {
lines++;
s = reader.readLine();
if((lines - lineNumber) == 0) {
System.out.println(s);
System.exit(0);
}
}
reader.close();
in.close();
}
// 文件内容的总行数。
static int getTotalLines(File file) throws IOException {
FileReader in = new FileReader(file);
LineNumberReader reader = new LineNumberReader(in);
String s = reader.readLine();
int lines = 0;
while (s != null) {
lines++;
s = reader.readLine();
if(lines=2){
if(s!=null){
System.out.println(s+"$");
}
}
}
reader.close();
in.close();
return lines;
}
}
java如何获取文件信息?
File 类是对文件和文件夹的抽象,包含了对文件和文件夹的多种属性和操作方法。File类的常用方法如下表:
返回
方法
说明
String getName 获取文件名称
String getParent 获取文件的父路径字符串
String getPath 获取文件的相对路径字符串
String getAbsolutePath 获取文件的绝对路径字符串
boolean exists 判断文件或者文件夹是否存在
boolean isFile 判断是不是文件类型
boolean isDirectory 判断是不是文件夹类型
boolean delete 删除文件或文件夹,如果删除成功返回结果为true
boolean mkdir 创建文件夹,创建成功返回true
boolean setReadOnly 设置文件或文件夹的只读属性
long length 获取文件的长度
long lastModified 获取文件的最后修改时间
String[ ] list 获取文件夹中的文件和子文件夹的名称,并存放到字符串数组中
Java 怎么获取文本域的值?
通过id找到文本域节点,通过value方式获取文本域的值。
1、找到文本域节点
var
ipt
=
document.getElementById('文本域id');//获取文本域的节点
关于java文本获取和java获取文本框的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-16,除非注明,否则均为
原创文章,转载请注明出处。