用java做

来源:百度知道 编辑:UC知道 时间:2024/05/10 07:10:59
3.文本文件in.txt中存放的是一组数据,编程统计正数、负数、零的个数,并将结果写入文本文件out.txt。(in.txt请自行创建)

import java.io.*;
public class Test {

public static void main(String[] args) {
try {
int z = 0; //正数的个数
int l = 0; //0的个数
int f = 0; //负数的个数
//in.txt中内容: 12,12,23,34,54,23,32,12,43,23,12,32,43,23,12,32,43,34,54,-123,-2,-4
FileReader reader = new FileReader("d:\\in.txt"); //读取文本中内容
BufferedReader br = new BufferedReader(reader);
String str = br.readLine();
String s[] = str.split(",");
for (int i = 0; i < s.length; i++) {
if(new Integer(s[i])==0){
l++;
}
if(new Integer(s[i])>0){
z++;
}
if(new Integer(s[i])<0){
f++;
}
}

FileOutputStream fos = new FileOutputStream("D:\\out.txt"); //创建文本out.txt
File nf = new File("d:\\out.txt");
FileWriter fw = new FileWriter(nf);
Buffer