导航:首页 > 编程语言 > java文件的输入输出

java文件的输入输出

发布时间:2023-02-21 22:04:33

java输入文件名,输出该文件的内容

java读取文件的内容,使用BufferedReader来读取,通过Scanner对象获取输入的文件名,来读取这个文件的内容并输出,具体示例代码如下:

publicclasstxttest{
/**
*读取txt文件的内容
*@paramfile想要读取的文件对象
*@return返回文件内容
*/
publicstaticStringtxt2String(Filefile){
Stringresult="";
try{
BufferedReaderbr=newBufferedReader(newFileReader(file));//构造一个BufferedReader类来读取文件
Strings=null;
while((s=br.readLine())!=null){//使用readLine方法,一次读一行
result=result+" "+s;
}
br.close();
}catch(Exceptione){
e.printStackTrace();
}
returnresult;
}

publicstaticvoidmain(String[]args){
Scannerscan=newScanner(System.in);
System.out.println("请输入文件名:");
Stringstr=scan.next();
Stringmulu="C:\Users\hp\Desktop\"+str+".txt";//文件具体目录
Filefile=newFile(mulu);
System.out.println(txt2String(file));
}
}

Ⅱ java实现文件的输入输出的重要性是什么

输入和输出功能是Java对程序处理数据能力的提高,Java以流的形式处理数据。流是一组有序的数据序列,根据操作的类型,分为输入流和输出流。

程序从输入流读取数据,向输出流写入数据。Java是面向对象的程序语言,每一个数据流都是一个对象,它们提供了各种支持“读入”与“写入”操作的流类。

Ⅲ java中读入和输出文本文件

/**
*测试3:从文本文件中读取数据
*/
staticvoidtestExample03(){
//1、在内存中打开要读取文件的字符流对象
try{
Readerreader=newFileReader("e:/ReadMe.log");
//2、从字符流中读取数据
//一次读取一个字符(麻烦)
/*intnum=reader.read();
System.out.println((char)num);
num=reader.read();
System.out.println((char)num);*/
//一次读取一个数组(必须确定数组的长度)
/*char[]cbuf=newchar[10];
reader.read(cbuf);
System.out.println(newString(cbuf));*/
//循环读取,一次就读一个
intch=reader.read();
StringBufferbuffer=newStringBuffer();
while(ch!=-1){//读取成功
buffer.append((char)ch);
ch=reader.read();
}
System.out.println(buffer.toString());
//3、关闭流
reader.close();
}catch(FileNotFoundExceptione){
System.out.println("要读取的文件不存在:"+e.getMessage());
}catch(IOExceptione){
System.out.println("文件读取错误:"+e.getMessage());
}
}
/**
*测试4:向文本文件中写入数据
*/
staticvoidtestExample04(){
System.out.println("请输入内容:");
Stringtext=input.next();
try{
//1、打开流
Writerw=newFileWriter("e:/测试.txt",true);
//2、写入内容
w.write(text);
//3、关闭流
w.close();
}catch(IOExceptione){
System.out.println("文件写入错误:"+e.getMessage());
}
}
/**
*测试5:使用效率高的字符流读写数据
*/
staticvoidtestExample05(){
try{
//1、创建流对象
Readerreader=newFileReader("e:/ReadMe.log");
//构建高效流对象
BufferedReaderbuffReader=newBufferedReader(reader);

//2、读取一行字符串
Stringline=buffReader.readLine();
StringBufferbuffer=newStringBuffer();
while(line!=null){
buffer.append(line+" ");
line=buffReader.readLine();
}
System.out.println(buffer.toString());;
//3、关闭流
buffReader.close();
reader.close();

Writerw=newFileWriter("e:/NewReadMe.txt");
BufferedWriterbuffWriter=newBufferedWriter(w);

buffWriter.write(buffer.toString());

buffWriter.close();
w.close();
System.out.println("写入成功!");

}catch(FileNotFoundExceptione){
System.out.println("要读取的文件不存在:"+e.getMessage());
}catch(IOExceptione){
System.out.println("文件读取错误:"+e.getMessage());
}
}

Ⅳ java实现文件的输入输出的主要使用的类是什么

ava的输入输出功能来自java.io 包中的InputStream类、OutputStream类、Reader类和Writer类以及继承它们的各种子类。

Ⅳ java 文件输入与输出

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

是导入包,不导入包怎么用
File、Scanner这些类。

throws FileNotFoundException是抛出异常,是Java规定的,比如万一找不到文件怎么办。

调用wirte()方法将信息输出到 “文件输入输出javaout”里

Ⅵ JAVA的输入输出流是什么有几种

以下是个人理解。流可以分为字节流和字符流区别嘛,你去搞清楚字节和字符的区别就知道了。流,其实没必要认为太高深,字节流的输入流,就是InputStream,他有个read()方法,而且有很多重载read(byte[]b)什么的,就是把文件转换成字节,后一个方法就是把转换的字节放到一个byte数组中。例如你定义一个File file=new File("d:/test.txt");byte[]b=new byte[1024];FileInputStream fis=new FileInputStream(file);fis.read(b);fis.close();//b里现在就放的是从文件d:/test.txt读取的字节(当然这不太严谨,可能会有错误)//现在是输出流FileOutputStream fos=new FileOutputStream(new File("d:/target.txt"));//write方法这个是byte数组中的字节放到目的文件d:/target.txt中fos.write(b);fos.close();这个是很简单的写法,没考虑文件大小,出现数组越界千万别怪我,手敲代码很烦呢,可能有错误,自己慢慢体会,这个急不来

Ⅶ java文件输入输出

import java.util.*;
import java.io.*;
class Student {
private String name;
private int age;
private int javaScore;
private int cScore;

public Student(String name,int age) {
this.name = name;
this.age = age;
}

public void setJavaScore(int java) {
if(java < 101 && java >= 0) {
this.javaScore = java;
}else{
System.out.println("Wrong score !");
}

}

public int getJavaScore() {
return this.javaScore;
}

public void setCScore(int C) {
if(C < 101 && C >= 0) {
this.cScore = C;
}else{
System.out.println("Wrong score !");
}

}

public int getCScore() {
return this.cScore;
}

public String toString() {
return this.name+" "+this.age+" "+this.javaScore+" "+this.cScore+"\n";
}
}
class TestChengji {
public static void main(String [] args) throws Exception{
Student[] StudentInfo = new Student[3];
Scanner in = new Scanner(System.in);
System.out.println("请输入学生姓名 年龄 c语言成绩与java的成绩,中间用空格隔开:");
System.out.println("例如: 小明 18 82 80");
for(int i=0;i<3;i++) {
System.out.println("请输入数据:");
StudentInfo[i] = new Student(in.next(),in.nextInt());
StudentInfo[i].setCScore(in.nextInt());
StudentInfo[i].setJavaScore(in.nextInt());
}
in.close();
BufferedWriter writer = new BufferedWriter(new FileWriter("chengji.txt"));
for(int i=0;i<3;i++) {
writer.write(StudentInfo[i].toString());
writer.flush();
}
writer.close();
}
}
//试试

阅读全文

与java文件的输入输出相关的资料

热点内容
文件夹对话框全屏模式怎么设置 浏览:542
中合产融app怎么下载 浏览:932
单片机保护断点设置 浏览:906
vba编程环境 浏览:345
推特app怎么变成英文了 浏览:453
车上mp3在哪个文件夹 浏览:618
单片机可调节六位数码管 浏览:896
服务器转速怎么表示 浏览:389
安卓三星手机怎么截屏 浏览:98
程序员高级算法 浏览:127
pythonbinlog 浏览:429
编译原理箭头上面一个加号乘号 浏览:664
螺杆式无油压缩机 浏览:996
代码编译要多久 浏览:333
领导错误命令怎么处理 浏览:948
怎么看手机各app内存 浏览:477
程序员栽在背景调查 浏览:780
什么是车场服务器 浏览:910
手机服务器怎么上网 浏览:998
linuxtime命令 浏览:859