「类的文件输出输入java」对文件的输入和输出
今天给各位分享类的文件输出输入java的知识,其中也会对对文件的输入和输出进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
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文件输入输出
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;i3;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;i3;i++) {
writer.write(StudentInfo[i].toString());
writer.flush();
}
writer.close();
}
}
//试试
Java中如何实现文件的输入和输出?
程序如下:
span style="color:#990000;"
/spanFile file1 = new File("/home/a123/a");
if (file1.exists()) {
System.out.println("存在文件夹a");
} else {
file1.mkdir(); // 文件夹的创建 创建文件夹/home/a123/a
}
File file2 = new File("/home/a123/a/test");
if (file2.exists()) {
System.out.println("存在文件夹或者文件test");
} else {
try {
file2.createNewFile(); // 文件的创建,注意与文件夹创建的区别
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 最简单的文件读写方法是使用类FileWriter
* (它的父类依次是java.io.OutputStreamWriter——java.io.Writer——java.lang.Object );
*/
// 下面是向文件file2里面写数据
try {
FileWriter fileWriter = new FileWriter(file2);
String s = new String("This is a test! \n" + "aaaa");
fileWriter.write(s);
fileWriter.close(); // 关闭数据流
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* 这样写数据的话,是完全更新文件test里面的内容,即把以前的东西全部删除,重新输入。
* 如果不想删除以前的数据,而是把新增加的内容增添在文件末尾处。只需要在创建FileWriter对象时候,使用另外一个构造函数即可:
* FileWriter fileWriter=new FileWriter(file2,true);
*/
// 下面是从文件file2读东西
try {
FileReader fileReader = new FileReader(file2);
String s = null;
char ch;
try {
char[] c = new char[100];
fileReader.read(c,0,2); // 具体想得到文件里面的什么值(单个char?int?还是String?),
System.out.println(c);
fileReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* 具体想得到文件里面的什么值(单个char?int?还是String?),需要知道不通read的不同用法:
* 1. int read() 读取单个字符。
* 2. int read(char[] cbuf) 将字符读入数组。 可以再将字符型数组转化位字符串
* 3. int read(char[] cbuf,int off,int len) 将字符读入数组的某一部分。
* 这三个方法都返回一个int值,作用是:读取的字符数,如果已到达流的末尾,则返回 -1.
*/
}
Java如何从文件输入输出??
// 读取文件
public class ReadTextFileExample {
public static void main(String[] args ) {
File file = new File("d:\\caipiao_3.txt");
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
// repeat until all lines is read
while ((text = reader.readLine()) != null) {
contents.append(text).append(System.getProperty("line.separator"));
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (reader != null) {
reader.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
// show file contents here
System.out.println(contents.toString());
}
}
// 写出文件
public class WriteTextFileExample {
public static void main(String[] args ) {
FileOutputStream fos = null;
BufferedWriter bw = null;
try {
File file = new File("abc.txt");
fos = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(fos));
bw.write("this is a sample!");
}
catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
finally {
try {
if (bw != null)
bw.close();
if (fos != null)
fos.close();
}
catch (IOException ie) {
}
}
}
}
java实现文件的输入输出的主要使用的类是什么?
ava的输入输出功能来自java.io 包中的InputStream类、OutputStream类、Reader类和Writer类以及继承它们的各种子类。
关于类的文件输出输入java和对文件的输入和输出的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-14,除非注明,否则均为
原创文章,转载请注明出处。