Ⅰ 关于java文件的操作。

代码

package com.util.string;

import java.io.*;

public class CountString {

public static int count(String filename, String target)
throws FileNotFoundException, IOException {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
StringBuilder strb = new StringBuilder();
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
strb.append(line);
}
String result = strb.toString();
int count = 0;
int index = 0;
while (true) {
index = result.indexOf(target, index + 1);
if (index > 0) {
count++;
} else {
break;
}
}
br.close();
return count;
}

public static void main(String[] args) {
try {
System.out.println(count("D:\\test.txt", "a"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Ⅱ 求代码:java文件操作

这个问题不是很难,代码不长,如果有不懂的,可以发消息过来:

import java.io.*;

public class ReadTest {
public static void main(String[] args) throws Exception {
File f=new File("d:\\xxx.xrdml");
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f.getParent()+f.getName().replace(".","_2."))));
String tmp=new String();
while((tmp=br.readLine())!=null){
if(tmp.contains("<startPosition>") || tmp.contains("</startPosition>")){
double num=Double.parseDouble(tmp.substring(tmp.indexOf("<startPosition>")+"<startPosition>".length(),tmp.indexOf("</startPosition>")));
num=num/2;
tmp=tmp.substring(0,tmp.indexOf("<startPosition>")+"<startPosition>".length())+num+tmp.substring(tmp.indexOf("</startPosition>"));
}
bw.write(tmp);
bw.newLine();
}
br.close();
bw.close();
}
}

Ⅲ java中对文件进行读写操作的基本类是什么

Java.io包中包括许多类提供许多有关文件的各个方面操作。
1 输入输出抽象基类InputStream/OutputStream ,实现文件内容操作的基本功能函数read()、 write()、close()、skip()等;一般都是创建出其派生类对象(完成指定的特殊功能)来实现文件读写。在文件读写的编程过程中主要应该注意异常处理的技术。
2 FileInputStream/FileOutputStream:
用于本地文件读写(二进制格式读写并且是顺序读写,读和写要分别创建出不同的文件流对象);
本地文件读写编程的基本过程为:
① 生成文件流对象(对文件读操作时应该为FileInputStream类,而文件写应该为FileOutputStream类);
② 调用FileInputStream或FileOutputStream类中的功能函数如read()、write(int b)等)读写文件内容;
③ 关闭文件(close())。
3 PipedInputStream/PipedOutputStream:
用于管道输入输出(将一个程序或一个线程的输出结果直接连接到另一个程序或一个线程的输入端口,实现两者数据直接传送。操作时需要连结);
4管道的连接:
方法之一是通过构造函数直接将某一个程序的输出作为另一个程序的输入,在定义对象时指明目标管道对象
PipedInputStream pInput=new PipedInputStream();
PipedOutputStream pOutput= new PipedOutputStream(pInput);
方法之二是利用双方类中的任一个成员函数 connect()相连接
PipedInputStream pInput=new PipedInputStream();
PipedOutputStream pOutput= new PipedOutputStream();
pinput.connect(pOutput);
5 管道的输入与输出:
输出管道对象调用write()成员函数输出数据(即向管道的输入端发送数据);而输入管道对象调用read()成员函数可以读起数据(即从输出管道中获得数据)。这主要是借助系统所提供的缓冲机制来实现的。
6随机文件读写:
RandomAccessFile类(它直接继承于Object类而非InputStream/OutputStream类),从而可以实现读写文件中任何位置中的数据(只需要改变文件的读写位置的指针)。
随机文件读写编程的基本过程为:
① 生成流对象并且指明读写类型;
② 移动读写位置;
③ 读写文件内容;
④ 关闭文件。

七里河团队答疑助人,希望我的回答对你有所帮助

Ⅳ java里的文件操作

TextFileOutputDemo Part 1
public static void main(String[] args)
{
PrintWriter outputStream = null;
try
{
outputStream =
new PrintWriter(new FileOutputStream("out.txt"));
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file out.txt.");
System.exit(0);
}
----------------------------------------
//: c11:IOStreamDemo.java
// Typical I/O stream configurations.
import java.io.*;
public class IOStreamDemo {
// Throw exceptions to console:
public static void main(String[] args)
throws IOException {
// 1. Reading input by lines:
BufferedReader in =
new BufferedReader(
new FileReader("IOStreamDemo.java"));
String s, s2 = new String();
while((s = in.readLine())!= null)
s2 += s + "\n";
in.close();
----------------------------------------
// 1b. Reading standard input:
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter a line:");
System.out.println(stdin.readLine());
// 2. Input from memory
StringReader in2 = new StringReader(s2);
int c;
while((c = in2.read()) != -1)
System.out.print((char)c);
--------------------------------------------
// 3. Formatted memory input
try {DataInputStream in3 =new DataInputStream(
new ByteArrayInputStream(s2.getBytes()));
while(true)
System.out.print((char)in3.readByte());
} catch(EOFException e) {
System.err.println("End of stream");
} // 4. File output
try { BufferedReader in4 =
new BufferedReader( new StringReader(s2));
PrintWriter out1 =
new PrintWriter(new BufferedWriter(
new FileWriter("IODemo.out")));
int lineCount = 1;
while((s = in4.readLine()) != null )
out1.println(lineCount++ + ": " + s);
out1.close();
} catch(EOFException e) {
System.err.println("End of stream");
}
------------------------------------------
// 5. Storing & recovering data
try { DataOutputStream out2 =
new DataOutputStream(new BufferedOutputStream(
new FileOutputStream("Data.txt")));
out2.writeDouble(3.14159);
out2.writeChars("That was pi\n");
out2.writeBytes("That was pi\n"); out2.close();
DataInputStream in5 = new DataInputStream(
new BufferedInputStream(
new FileInputStream("Data.txt")));
BufferedReader in5br =
new BufferedReader(new InputStreamReader(in5));
// Must use DataInputStream for data:
System.out.println(in5.readDouble());
// Can now use the "proper" readLine():
System.out.println(in5br.readLine());
System.out.println(in5br.readLine());
} catch(EOFException e) {
System.err.println("End of stream");}
-------------------------------------------------
// 6. Reading/writing random access files
RandomAccessFile rf =
new RandomAccessFile("rtest.dat", "rw");
for(int i = 0; i < 10; i++)
rf.writeDouble(i*1.414);
rf.close();
rf = new RandomAccessFile("rtest.dat", "rw");
rf.seek(5*8);
rf.writeDouble(47.0001);
rf.close();
rf = new RandomAccessFile("rtest.dat", "r");
for(int i = 0; i < 10; i++)
System.out.println(
"Value " + i + ": " +
rf.readDouble());
rf.close();
}
}
--------------------------------------------------
//: c11:GZIPcompress.java
// Uses GZIP compression to compress a file
// whose name is passed on the command line.
import java.io.*;
import java.util.zip.*;
public class GZIPcompress {
// Throw exceptions to console:
public static void main(String[] args)
throws IOException {
BufferedReader in =
new BufferedReader(
new FileReader(args[0]));
BufferedOutputStream out =
new BufferedOutputStream(
new GZIPOutputStream(
new FileOutputStream("test.gz")));
System.out.println("Writing file");
int c;
while((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
System.out.println("Reading file");
BufferedReader in2 =
new BufferedReader(
new InputStreamReader(
new GZIPInputStream(
new FileInputStream("test.gz"))));
String s;
while((s = in2.readLine()) != null)
System.out.println(s);
}
}

Ⅳ java文件操作问题

就是说你可以对文件的内容进行更改,比如说把所有a变成c,b变成d...,这就叫加密,解密可以把c还原成a...
当然加密,解密算法可以设计的复杂点。

Ⅵ JAVA文件操作问题

建立文件输入流
用while作循环,用readline一行一行读取,存入str字符串,用indexof,返回,xx位置,
把str分两段,删除xx字符串,合成新字符串,建立输出流,将新字符串存入,新建文件
跳出循环
完成操作

Ⅶ java文件操作

区别是有的:
1、效率上考虑:
第一种方法要高很多,因为.flush();在起作用,他的具体作用其他人都说的很好了。

2、大小上:
这个不会有任何的区别,反正就这么大的文件,怎么读写都是这些。

3、安全上:
推荐第二种,第二种每读取一行,就写入文件中一行,所以当程序意外终止(不如停电等),那么读取的数据也会被保存。而第一种就不会保存了(除了系统自动保存的,那样不一定什么时候保存,随系统不同而不同)。而且采用第二种方法,如果你愿意,你可以加入一个文件续传功能,就是可以继续的传输数据,而不用重新的在全部读取,写入,只需要找到已经写入的写入点,然后在那之后读取写入即可~~~

为了达到效率与安全的双方面考虑,可以试试在while中加入每10次或N次,就执行一次flush();方法。

Ⅷ JAVA文件操作的问题

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class OpText {

public static void main(String[] args) throws IOException{
File f = new File("d:\\file\\file.txt");
if(!f.exists()){
System.out.println("文件不存在!");
return;
}
// 累加数字偶数
int number = 0;
// 时间差累计结果
long timeAdded = 0;
// 累加字符串
StringBuffer strAppend = new StringBuffer();
InputStream input = new FileInputStream(f);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));

String value = reader.readLine();
while(value != null){
try{
int n = Integer.parseInt(value.trim());
if(n%2 == 0){
number += n;
}
}catch(Exception e){
if(value.indexOf("-")>0){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String standard = "2007-01-01";
try {
Date d = format.parse(value.trim());
Date standardDate = format.parse(standard);
timeAdded = timeAdded+ d.getTime()-standardDate.getTime();
} catch (ParseException e1) {
System.out.println("日期格式不正确!");
e1.printStackTrace();
}
}else{
if(value.length()>8){
strAppend.append(value.substring(1,8));
}else{
strAppend.append(value);
}
}
}
value = reader.readLine();
}

OutputStream output = new FileOutputStream("d:\\file\\file.txt",true);
output.write(("\r\n时间差:"+timeAdded+"\r\n").getBytes());
output.write(("偶数和:"+number+"\r\n").getBytes());
output.write(("字符串:"+strAppend.toString()).getBytes());

output.close();
reader.close();
input.close();
}
}

Ⅸ java 文件操作

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

public class Test {
public static void main(String[] args) throws IOException {
Desktop desktop = Desktop.getDesktop();
desktop.edit(new File("你要抄打袭开的文件路径"));
}
}

Ⅹ JAVA文件操作

第一个明白,使用Filter可以实现,
第二个不明白,是找历史记录,还是怎么的?