java文件输入
① 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中向文件写入数据的问题
可以使用java中的FileWriter类向文件中写入数据。很简单。代码例子如下:
importjava.io.FileWriter;
importjava.io.IOException;
publicclassFilewriter{
privatestaticfinalStringLINE_SEPARATOR=System.getProperty("line.separator");
/**
*
*@paramargs
*@throwsIOException
*/
publicstaticvoidmain(String[]args)throwsIOException{
/**
*创建一个可以往文件中写入字符数据的字符流输出流对象
*创建时必须明确文件的目的地
*如果文件不存在,这回自动创建。如果文件存在,则会覆盖。
*当路径错误时会抛异常
*
*当在创建时加入true参数,回实现对文件的续写。
*/
FileWriterfw=newFileWriter("C:\demo1.txt",false);
/**
*调用该对象的write方法,向文件写入字符。
*
*其实写入到了临时存储缓冲区中
*/
fw.write("hello world!");//windows中的换行为 unix下为 。
fw.write("hahaha");
/**
*进行刷新,将字符写到目的地中。
*/
//fw.flush();
/**
*关闭流,关闭资源。在关闭前会调用flush方法刷新缓冲区。关闭后在写的话,会抛IOException
*/
fw.close();
}
}
③ java输入如何输入到文件中并用界面显示出来
//代码很简单我就不加注释了。因为键盘输入的话是采集不到空格,没有加入Scanner
public class Test2 {
public static void main(String[] args) {
int a = 0, b = 0, c = 0,d=0;
int tem;
String str = "abcdABCD 123-0-=";
char[] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
tem=ch[i];
if (tem == 32) {
a++;
} else if(tem>=65&&tem<=90||tem>=97&&tem<120){
b++;
} else if(tem>=48&&tem<=57){
c++;
}else{d++;}
}System.out.println("空格数:"+a);
System.out.println("字母数:"+b);
System.out.println("数字数:"+c);
System.out.println("其他字符"+d);
}
}
④ Java中如何实现文件的输入输出
程序如下:
<span style="color:#990000;">
</span>File 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如何使用文件字节输入流类 : FileInputStream类
文件字节输入流可以从指定路径的文件中读取字节数据。文件字节输入流类继承InputStream类,并实现了读取输入流的各种方法。
创建文件字节输入流创建的构造方法语法如下:
语法1:以File对象为参数创建FileInputStream实例
- new FileInputStream(File file)1
语法2:以文件对象的绝对路径为参数创建FIleInputStream实例
- new FileInputStream(String filepath)
⑥ java中如何实现用键盘输入内容到文件
step1:新建一个演示类demo
运行结果1:
please input “学生姓名”
李小明
please input “科目名称”
数学
please input“科目成绩”
98
学生个人信息
姓名:李小明
科目:数学
成绩:98.0
等级:A+
运行结果2:
please input “学生姓名”
王强
please input “科目名称”
英语
please input“科目成绩”
52
学生个人信息
姓名:王强
科目:英语
成绩:52.0
等级:D
⑦ Java中如何实现文件的输入和输出
程序如下:
<span style="color:#990000;">
</span>File 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实现文件的输入输出的重要性是什么
输入和输出功能是Java对程序处理数据能力的提高,Java以流的形式处理数据。流是一组有序的数据序列,根据操作的类型,分为输入流和输出流。
程序从输入流读取数据,向输出流写入数据。Java是面向对象的程序语言,每一个数据流都是一个对象,它们提供了各种支持“读入”与“写入”操作的流类。
⑨ java中如何做文件字节输入流
首先,我以往经验是写入字节流和输出字节流的方式一定要一致,不然写入的字节流就会出错甚至读取不到,下面是字节流的写入和读取方法:
importjava.io.*;
classTest{
publicstaticvoidmain(Stringargs[]){
FileInputStreamfis=null;
FileOutputStreamfos=null;
byte[]buffer=newbyte[100];
inttemp=0;
try{
fis=newFileInputStream("D:/wenhao/src/from.txt");
fos=newFileOutputStream("D:/wenhao/src/to.txt");
while(true){
temp=fis.read(buffer,0,buffer.length);
if(temp==-1){
break;
}
fos.write(buffer,0,temp);
}
}
catch(Exceptione){
System.out.println(e);
}
finally{
try{
fis.close();
fos.close();
}
catch(Exceptione2){
System.out.println(e2);
}
}
}
}