「javafile行」javaFile类
本篇文章给大家谈谈javafile行,以及javaFile类对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java获取文本文件中的某行
- 2、Java 如何修改文件的某一行内容
- 3、如何用java按行读取文本文件
- 4、java怎么读取txt文件的行数
- 5、如何用Java实现向一个file指定位置写入多行数据,每行数据要指定开始的p
- 6、java读取文本文件后怎样算出文本文件的行数
java获取文本文件中的某行
public static GpsData createGpsData(String gprmcStr){
GpsData gpsData = new GpsData() ;
try {
String encoding="GBK";
File file=new File(gprmcStr);
if(file.isFile() file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
int pos = lineTxt.indexOf("$GPRMC");
if(pos!=-1){
for (int i = 0;i 11;i++){
String value = lineTxt.substring(0,lineTxt.indexOf(",")) ;
lineTxt = lineTxt. substring(lineTxt.indexOf(",") + 1) ;
switch (i){
case 0:gpsData. setType(value) ;break;
case 1:gpsData. setTime(value) ;break;
case 2:gpsData. setStatus(value) ;break;
case 3:gpsData. setLat(value) ;break;
case 4:gpsData. setLatdir(value) ;break;
case 5:gpsData. setLon(value) ;break;
case 6:gpsData. setLondir(value) ;break;
case 7:gpsData. setSpeedKN(value) ;break;
case 8:gpsData. setTrackTure(value) ;break;
case 9:gpsData. setDate(value) ;break;
case 10:gpsData. setMagneticDirection(value) ;break;
}
}
System.out.println("Type="+gpsData.getType()
+", Time="+gpsData.getTime()
+", Status="+gpsData.getStatus()
+", Lat="+gpsData.getLat()
+", Latdir="+gpsData.getLatdir()
+", Lon="+gpsData.getLon()
+", setLondir="+gpsData.getLondir()
+", SpeedKN="+gpsData.getSpeedKN()
+", TrackTure="+gpsData.getTrackTure()
+", Date="+gpsData.getDate()
+", MagneticDirec="+gpsData.getMagneticDirection());
}
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return gpsData ;
}
Java 如何修改文件的某一行内容
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Day02_B {
static String path="K:/Test/Name.txt";//路径
public static void main(String[] args) {
File fileText=new File(path);//文件
if(fileText.canExecute()) //如果文件存在就继续
setText(fileText,"刚","xx");//“刚”指定改为:“XX”
}
private static void setText(File fileText,String target,String src) {//修改
BufferedReader br=null;
PrintWriter pw=null;
StringBuffer buff=new StringBuffer();//临时容器!
String line=System.getProperty("line.separator");//平台换行!
try {
br=new BufferedReader(new FileReader(fileText));
for(String str=br.readLine();str!=null;str=br.readLine()) {
if(str.contains(target))
str=str.replaceAll(target,src);
buff.append(str+line);
}
pw=new PrintWriter(new FileWriter(fileText),true);
pw.println(buff);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
if(br!=null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
if(pw!=null)
pw.close();
}
}
}
如何用java按行读取文本文件
File file = new File("文件地址");
Scanner scanner = new Scanner(file);
String lineContent = null;
while(scanner.hasNextLine()){//如果有下一行
lineContent = scanner.nextLine();//读取下一行内容
}
scanner.close();//关闭Scanner
java怎么读取txt文件的行数
import java.io.File;
import java.io.RandomAccessFile;
/**
* 读取文档的第二行内容
*
* @author 3306 2017年3月21日
* @see
* @since 1.0
*/
public class CountLine {
/*
* 读取文件绝对路径
*/
private static String filePath = "d:/cms.sql";
public static void main(String[] args) {
long line = readLine(filePath);
System.out.println(line);
}
/**
* 读取文件行数
*
* @param path
* 文件路径
* @return long
*/
public static long readLine(String path) {
long index = 0;
try {
RandomAccessFile file = new RandomAccessFile(new File(path), "r");
while (null != file.readLine()) {
index++;
}
file.close();
} catch (Exception e) {
e.printStackTrace();
}
return index;
}
}
如何用Java实现向一个file指定位置写入多行数据,每行数据要指定开始的p
Java使用FileWriter实现文件的写入,用法为:FileWriter(file,true); 其中第二个参数设置成false就是覆盖写入,true就是增量存储。
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; //返回总的行数
}
相信看完上面的,应该就会了。
关于javafile行和javaFile类的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。