一、java怎么读取文档中指定内容
package com.lwj.demo;
import java.io.*;
public class RandomAccessFileDemo {
public static void main(String[] args) throws Exception {
RandomAccessFile file = new RandomAccessFile("file", "rw");
// 以下向file文件中写数据
file.writeInt(20);// 占4个字节
file.writeDouble(8.236598);// 占8个字节
file.writeUTF("这是一个UTF字符串");// 这个长度写在当前文件指针的前两个字节处,可用readShort()读取
file.writeBoolean(true);// 占1个字节
file.writeShort(395);// 占2个字节
file.writeLong(2325451l);// 占8个字节
file.writeUTF("又是一个UTF字符串");
file.writeFloat(35.5f);// 占4个字节
file.writeChar('a');// 占2个字节
file.seek(0);// 把文件指针位置设置到文件起始处
// 以下从file文件中读数据,要注意文件指针的位置
System.out.println("——————从file文件指定位置读数据——————");
System.out.println(file.readInt());
System.out.println(file.readDouble());
System.out.println(file.readUTF());
file.skipBytes(3);// 将文件指针跳过3个字节,本例中即跳过了一个boolean值和short值。
System.out.println(file.readLong());
file.skipBytes(file.readShort()); // 跳过文件中“又是一个UTF字符串”所占字节,注意readShort()方法会移动文件指针,所以不用加2。
System.out.println(file.readFloat());
}
}
二、JAVA读取指定文件里面的指定内容
给你写了一个小方法,应该满足你的要求了://url是你要读取的文件的路径,wanted是所要求的包含的字符串如这里是“COMMON.9006 - 000332”。
public static void readWantedText(String url, String wanted) { try { FileReader fr = new FileReader(url); BufferedReader br = new BufferedReader(fr); String temp = "";// 用于临时保存每次读取的内容 while (temp != null) { temp = br.readLine(); if (temp != null && temp.contains(wanted)) { System.out.println(temp); } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }用的话直接调用这个方法就可以了:例如readWantedText("D:\\test.txt", "COMMON.9006 - 000332");//注意java路径需要在每条\前面在加条\表示转义。
三、JAVA读取指定文件里面的指定内容
给你写了一个小方法,应该满足你的要求了:
//url是你要读取的文件的路径,wanted是所要求的包含的字符串如这里是“COMMON.9006 - 000332”。
public static void readWantedText(String url, String wanted) {
try {
FileReader fr = new FileReader(url);
BufferedReader br = new BufferedReader(fr);
String temp = "";// 用于临时保存每次读取的内容
while (temp != null) {
temp = br.readLine();
if (temp != null && temp.contains(wanted)) {
System.out.println(temp);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
用的话直接调用这个方法就可以了:例如
readWantedText("D:\\test.txt", "COMMON.9006 - 000332");
//注意java路径需要在每条\前面在加条\表示转义。
四、java读取指定目录下的文件内容
public class ReadFromFile { /** * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*/ public static void readFileByBytes(String fileName) { File file = new File(fileName); InputStream in = null; try { System.out.println("以字节为单位读取文件内容,一次读一个字节:"); // 一次读一个字节 in = new FileInputStream(file); int tempbyte; while ((tempbyte = in.read()) != -1) { System.out.write(tempbyte); } in.close(); } catch (IOException e) { e.printStackTrace(); return; } try { System.out.println("以字节为单位读取文件内容,一次读多个字节:"); // 一次读多个字节 byte[] tempbytes = new byte[100]; int byteread = 0; in = new FileInputStream(fileName); ReadFromFile.showAvailableBytes(in); // 读入多个字节到字节数组中,byteread为一次读入的字节数 while ((byteread = in.read(tempbytes)) != -1) { System.out.write(tempbytes, 0, byteread); } } catch (Exception e1) { e1.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e1) { } } } } /** * 以字符为单位读取文件,常用于读文本,数字等类型的文件 */ public static void readFileByChars(String fileName) { File file = new File(fileName); Reader reader = null; try { System.out.println("以字符为单位读取文件内容,一次读一个字节:"); // 一次读一个字符 reader = new InputStreamReader(new FileInputStream(file)); int tempchar; while ((tempchar = reader.read()) != -1) { // 对于windows下,\r\n这两个字符在一起时,表示一个换行。 // 但如果这两个字符分开显示时,会换两次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
if (((char) tempchar) != '\r') { System.out.print((char) tempchar); } } reader.close(); } catch (Exception e) { e.printStackTrace(); } try { System.out.println("以字符为单位读取文件内容,一次读多个字节:"); // 一次读多个字符 char[] tempchars = new char[30]; int charread = 0; reader = new InputStreamReader(new FileInputStream(fileName)); // 读入多个字符到字符数组中,charread为一次读取字符数 while ((charread = reader.read(tempchars)) != -1) { // 同样屏蔽掉\r不显示 if ((charread == tempchars.length) && (tempchars[tempchars.length - 1] != '\r')) { System.out.print(tempchars); } else { for (int i = 0; i < charread;="" i++)="" {="" if="" (tempchars[i]="=" '\r')="" {="" continue;="" }="" else="" {="" system.out.print(tempchars[i]);="" }="" }="" }="" }="" }="" catch="" (exception="" e1)="" {="" e1.printstacktrace();="" }="" finally="" {="" if="" (reader="" !="null)" {="" try="" {="" reader.close();="" }="" catch="" (ioexception="" e1)="" {="" }="" }="" }="" }="" *="" *="" 以行为单位读取文件,常用于读面向行的格式化文件="" */="" public="" static="" void="" readfilebylines(string="" filename)="" {="" file="" file="new" file(filename);="" bufferedreader="" reader="null;" try="" {="" system.out.println("以行为单位读取文件内容,一次读一整行:");="" reader="new" bufferedreader(new="" filereader(file));="" string="" tempstring="null;" int="" line="1;" 一次读入一行,直到读入null为文件结束="" while="" ((tempstring="reader.readLine())" !="null)" {="" 显示行号="" system.out.println("line="" "="" +="" line="" +="" ":="" "="" +="" tempstring);="" line++;="" }="" reader.close();="" }="" catch="" (ioexception="" e)="" {="" e.printstacktrace();="" }="" finally="" {="" if="" (reader="" !="null)" {="" try="" {="" reader.close();="" }="" catch="" (ioexception="" e1)="" {="" }="" }="" }="" }="" *="" *="" 随机读取文件内容="" */="" public="" static="" void="" readfilebyrandomaccess(string="" filename)="" {="" randomaccessfile="" randomfile="null;" try="" {="" system.out.println("随机读取一段文件内容:");="" 打开一个随机访问文件流,按只读方式="" randomfile="new" randomaccessfile(filename,="" "r");="" 文件长度,字节数="" long="" filelength="randomFile.length();" 读文件的起始位置="" int="" beginindex="(fileLength"> 4) ? 4 : 0; // 将读文件的开始位置移到beginIndex位置。 randomFile.seek(beginIndex); byte[] bytes = new byte[10]; int byteread = 0; // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
// 将一次读取的字节数赋给byteread while ((byteread = randomFile.read(bytes)) != -1) { System.out.write(bytes, 0, byteread); } } catch (IOException e) { e.printStackTrace(); } finally { if (randomFile != null) { try { randomFile.close(); } catch (IOException e1) { } } } } /** * 显示输入流中还剩的字节数 */ private static void showAvailableBytes(InputStream in) { try { System.out.println("当前字节输入流中的字节数为:" + in.available()); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { String fileName = "C:/temp/newTemp.txt"; ReadFromFile.readFileByBytes(fileName); ReadFromFile.readFileByChars(fileName); ReadFromFile.readFileByLines(fileName); ReadFromFile.readFileByRandomAccess(fileName); } }。
五、java怎样读取文件的指定地方内容
例如
import java.io.*;
public class Example21_1
{
public static void main(String args[])
{
String reading;
try
{
byte buf[]=new byte[1024];
RandomAccessFile fileOb=new RandomAccessFile("f:\\file\\test.txt","rw");
fileOb.readLine();
fileOb.getFilePointer();
reading=fileOb.readLine();
System.out.println(reading);
fileOb.readLine();
fileOb.readLine();
reading=fileOb.readLine();
System.out.println(reading);
}
catch(IOException e){}
}
}
希望对你有帮助
转载请注明出处51数据库 » java读取word指定内容
老娘真的信了你邪