Java文件写入与读取实例求最大子数组
出现bug的点:输入数组无限大;
输入的整数,量大:
解决方案:向文件中输入随机数组,大小范围与量都可以控制。
源代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Random;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Random;
public class XieRuDuQu {
public static void main(String[] args) throws IOException{
writeRandomByOutputStreamWriter("D:\\data.txt");
System.out.println("最大子数组:"+max(readArrayByInputStreamReader("D:\\data.txt")));//输出最大子数组
}
public static int max(int a[]) {
/**
* 求一维数组最大子数组
*/
int x=a.length;
int b[][]=new int[x][x];//存子数组
int c[]=new int[x];//存二维数组每一行的最大值
for(int i=0;i<x;i++) {//所有子数组存入二维数组中:以第i个开头的子数组们存入第i行
for(int j=0;j<x;j++) {//求出二维数组的一行
int sum=0;
for(int s=i;s<=j;s++){//求每一个子数组
sum+=a[s];
}
b[i][j]=sum;//存子数组
}
};
for(int i=0;i<x;i++) {//i为行
for(int j=0;j<x;j++) {//j为列
if(b[i][j]>c[i]) {
c[i]=b[i][j];
}
}
}
int s=0;
for(int i=0;i<c.length;i++) {
if(s<c[i]) {
s=c[i];
}
};
return s;
}
public static int[] readArrayByInputStreamReader(String txt) {
/**
*读取
*/
ArrayList<String> arrayList = new ArrayList<>();
try {
String encoding = "UTF-8";
File file = new File(txt);
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
if (file.isFile() && file.exists()) {
String temp;
while ((temp = bufferedReader.readLine()) != null) {
arrayList.add(temp);
}
bufferedReader.close();
read.close();
}
}catch (Exception e) {
e.printStackTrace();
}
// 对ArrayList中存储的字符串进行处理
int length = arrayList.size();
int[] array = new int[length];
for (int i = 0; i < length; i++) {
String s = arrayList.get(i);
array[i] = Integer.parseInt(s);
}
return array;
}
public static void writeRandomByOutputStreamWriter(String txt) {
/**
* 文件写入随机数
*/
try {
String encoding = "UTF-8";
File file = new File(txt);
if(!file.exists())
{ //如果不存在data.txt文件则创建
file.createNewFile();
System.out.println("文件创建完成");
}
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file), encoding);
BufferedWriter bufferedWriter = new BufferedWriter(write);
//产生随机数据,写入文件
Random random = new Random();
for(int i=0;i<10;i++)
{
int randint =(int)Math.floor((random.nextDouble()*100.0)); //产生0-10000之间随机数
bufferedWriter.write(String.valueOf(randint)); //写入一个随机数
bufferedWriter.newLine(); //新的一行
}
bufferedWriter.close();
write.flush();
write.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException{
writeRandomByOutputStreamWriter("D:\\data.txt");
System.out.println("最大子数组:"+max(readArrayByInputStreamReader("D:\\data.txt")));//输出最大子数组
}
public static int max(int a[]) {
/**
* 求一维数组最大子数组
*/
int x=a.length;
int b[][]=new int[x][x];//存子数组
int c[]=new int[x];//存二维数组每一行的最大值
for(int i=0;i<x;i++) {//所有子数组存入二维数组中:以第i个开头的子数组们存入第i行
for(int j=0;j<x;j++) {//求出二维数组的一行
int sum=0;
for(int s=i;s<=j;s++){//求每一个子数组
sum+=a[s];
}
b[i][j]=sum;//存子数组
}
};
for(int i=0;i<x;i++) {//i为行
for(int j=0;j<x;j++) {//j为列
if(b[i][j]>c[i]) {
c[i]=b[i][j];
}
}
}
int s=0;
for(int i=0;i<c.length;i++) {
if(s<c[i]) {
s=c[i];
}
};
return s;
}
public static int[] readArrayByInputStreamReader(String txt) {
/**
*读取
*/
ArrayList<String> arrayList = new ArrayList<>();
try {
String encoding = "UTF-8";
File file = new File(txt);
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
if (file.isFile() && file.exists()) {
String temp;
while ((temp = bufferedReader.readLine()) != null) {
arrayList.add(temp);
}
bufferedReader.close();
read.close();
}
}catch (Exception e) {
e.printStackTrace();
}
// 对ArrayList中存储的字符串进行处理
int length = arrayList.size();
int[] array = new int[length];
for (int i = 0; i < length; i++) {
String s = arrayList.get(i);
array[i] = Integer.parseInt(s);
}
return array;
}
public static void writeRandomByOutputStreamWriter(String txt) {
/**
* 文件写入随机数
*/
try {
String encoding = "UTF-8";
File file = new File(txt);
if(!file.exists())
{ //如果不存在data.txt文件则创建
file.createNewFile();
System.out.println("文件创建完成");
}
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file), encoding);
BufferedWriter bufferedWriter = new BufferedWriter(write);
//产生随机数据,写入文件
Random random = new Random();
for(int i=0;i<10;i++)
{
int randint =(int)Math.floor((random.nextDouble()*100.0)); //产生0-10000之间随机数
bufferedWriter.write(String.valueOf(randint)); //写入一个随机数
bufferedWriter.newLine(); //新的一行
}
bufferedWriter.close();
write.flush();
write.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
文档:
运行结果: