十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
progressBar.set...
成都创新互联专业做网站、成都网站制作,集网站策划、网站设计、网站制作于一体,网站seo、网站优化、网站营销、软文平台等专业人才根据搜索规律编程设计,让网站在运行后,在搜索中有好的表现,专业设计制作为您带来效益的网站!让网站建设为您创造效益。
这些代码调用以后,看能不能跟着调用界面刷新的方法,refresh,update之类(不清楚java怎么写,我说的是C#的)
import java.io.*;
public class su {
public static void main(String args[]){
try {
BufferedReader r = new BufferedReader(new FileReader(new File("D:\\a.txt")));
PrintWriter w = new PrintWriter(new FileOutputStream(new File("D:\\b.txt")));
String line = null;
int count = 1;
while(true){
line = r.readLine();
if(line==null)break;
w.println(count+++line);
}
r.close();
w.close();
} catch (FileNotFoundException e) {
System.out.println("找不到文件或文件打开失败");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("未知异常");
}
}
}
那个也可以呀,我这个是按我平时的习惯写的,效果一样的。
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;
/**
*
* @author Felly
* @date 2008年12月29日16:16:12
*
*/
public class InOut {
//调试的时候文件和class文件在同目录,不然自己加绝对路径
public final static String inFile="in.txt";//被写文件
public final static String outFile="out.txt";//被读文件
public void readAndWrite(String in,String out) throws
FileNotFoundException,IOException {
File inFile=new File(in);
File outFile=new File(out);
BufferedReader reader=new BufferedReader(new FileReader(outFile));
PrintWriter pw=new PrintWriter(new FileWriter(inFile));
String line="";
int index=0;
while((line=reader.readLine())!=null)//循环到没有内容为止
{ index++;
line=index+line;//加上行号
pw.write(line);//写入一行
}
//close
pw.close();
reader.close();
}
}
你好,java的API中提供了用于对象输入输出文件的操作,实例代码如下:
定义单词类如下(注意:你定义的类要实现Serializable接口)
public class Words implements Serializable {
private int size;
private String[] words;
public Words(){};
public Words(String...strs){
this.words = strs;
this.size = strs.length;
}
@Override
public String toString() {
return "Words{" +
"size=" + size +
", words=" + Arrays.toString(words) +
'}';
}
}
2. 对象输入输出api测试类
public class ObjIOTest {
public static void main(String[] args) {
String path = "d:/myIOTest.txt";
ObjIOTest objIOTest = new ObjIOTest();
Words words = new Words("hello", "my", "dear", "friend");
try {
objIOTest.writeObject(path,words);
Words wordsFromFile = (Words)objIOTest.readObject(path);
System.out.println(wordsFromFile.toString());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
//java serialize a object to file
public void writeObject(String path,Object map) throws IOException{
File f=new File(path);
FileOutputStream out=new FileOutputStream(f);
ObjectOutputStream objwrite=new ObjectOutputStream(out);
objwrite.writeObject(map);
objwrite.flush();
objwrite.close();
}
// read the object from the file
public Object readObject(String path) throws IOException, ClassNotFoundException{
FileInputStream in=new FileInputStream(path);
ObjectInputStream objread=new ObjectInputStream(in);
Object map=objread.readObject();
objread.close();
return map;
}
}
把两段代码拷贝到一个包下即可运行了,希望您的问题得到解答
public class test {
public static void main(String[] args) throws IOException {
//输出流
FileOutputStream fos = new FileOutputStream("mytext.txt");
DataOutputStream dos = new DataOutputStream(fos);
//输入流
FileInputStream fis = new FileInputStream("mytext.txt");
DataInputStream dis = new DataInputStream(fis);
//随机种子
Random random = new Random();
int[] nums = new int[20];//存放20个数的数组
System.out.print("产生的随机数: ");
for (int i = 0; i 20; i++) {
nums[i] = random.nextInt(100);// 产生在0~99之间的随机数
System.out.print(nums[i]+" ,");
}
//冒泡法排序,从小到大
for (int i = 0; i nums.length; i++) {
for (int j = i; j nums.length; j++) {
if (nums[i] nums[j]) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
}
//写入文件
for (int i = 0; i nums.length; i++) {
dos.writeInt(nums[i]);
dos.flush();
}
System.out.print("\n从文件中读出数据: ");
//读出数据
for(int i=0;i20;i++)
{
int result = dis.readInt();
System.out.print(" ,"+result);
}
}
}
运行结果:
希望对题主有所帮助,望采纳!
代码如下:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class App {
public static void main(String[] args) throws FileNotFoundException, IOException {
try (
FileInputStream inputStream = new FileInputStream(new File("d:\\temp\\abc.txt"));
InputStreamReader streamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(streamReader);
FileOutputStream outputStream = new FileOutputStream(new File("d:\\temp\\output.txt"));
OutputStreamWriter streamWriter = new OutputStreamWriter(outputStream);
BufferedWriter writer = new BufferedWriter(streamWriter);
) {
int count = 1;
String line = null;
while ((line = reader.readLine()) != null) {
writer.write(count++ + ": " + line);
writer.newLine();
}
}
System.out.println("done.");
}
}