String test=args[0];提示数据越界为什么,好菜哦,都是细节

本文探讨了Java中数据越界异常的问题,通过一个简单的代码示例解释了当尝试访问数组中不存在的元素时,程序将抛出ArrayIndexOutOfBoundsException异常的原因。

String test=args[0];提示数据越界为什么

public class ExTestDrive {
public static void main(String[] args){
String test=args[0];

 

 

package com.itgaohe.iodemo.iopractice; import java.io.*; import java.util.Scanner; public class AutoLoad { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 自动登录 String parentDirctory = "src\\userinfo"; String childPath = "cookie.txt"; String sourcePath = "src\\userinfo\\test.txt"; String src = "src\\userinfo\\cookie.txt"; // 自动登录 if (autoLoad(src, sourcePath)) { System.out.println("自动登录成功"); } else { System.out.println("用户名或密码错误,登录失败"); // 手动登录 load(scanner,sourcePath,src); } } // 创建文件cookie文件方法 public static void create(String parentDirctory, String childPath) { File parent = new File(parentDirctory); parent.mkdirs(); File cookie = new File(parent, childPath); try { cookie.createNewFile(); } catch (IOException e) { e.printStackTrace(); } if (cookie.exists()) { System.out.println("创建成功"); } else { System.out.println("创建失败"); } } //读取cookie文件件内容 public static String[] readXXX(String src) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(src)); String saveName = br.readLine(); String savePassword = br.readLine(); return new String[]{saveName, savePassword}; } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } // 从userInfo\\test读取信息 public static String[] readTest(String sourcePath) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(sourcePath)); String s = br.readLine(); String[] temp = s.split("&"); String name = temp[0].split("=")[1]; String password = temp[1].split("=")[1]; return new String[]{name, password}; } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } // 手动登录验证 public static void load(Scanner scanner, String sourcePath,String src) { String readdata[] = readTest(sourcePath); String inputName= null; String inputPassword = null; if (readdata != null) { String trueName = readdata[0]; String truePassword = readdata[1]; System.out.println("欢迎登录系统"); System.out.println("请输入用户名"); inputName = scanner.nextLine().trim(); System.out.println("请输入密码"); inputPassword = scanner.nextLine().trim(); if (trueName.equals(inputName) && truePassword.equals(inputPassword)) { System.out.println("登录成功信息已保存,下次可自动登录"); } } else { System.out.println("登录失败,用户名或密码错误"); // 保存信息到cookie文件中 writeDate(src,inputName,inputPassword); } } //自动登录验证方法 public static boolean autoLoad(String src, String sourcePath) { // 读取cookie文件的信息 String[] readDate = readXXX(src); String saveName = null; String savePassword = null; if (readDate != null) { saveName = readDate[0]; savePassword = readDate[1]; System.out.println("自动登录中"); } //读取test文件信息 String[] test = readTest(sourcePath); String correctName = null; String correctPassword = null; if (test != null) { correctName = test[0]; correctPassword = test[1]; } // 验证信息是否一致 if (saveName.equals(correctName) && savePassword.equals(correctPassword)) { return true; } else { return false; } } // 将信息写入到cookie文件 public static void writeDate(String src,String username,String password){ BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(src)); bw.write("username="+username); bw.newLine(); bw.write("password="+password); } catch (IOException e) { e.printStackTrace(); }finally { if (bw != null) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } } 帮我检查修改一下代码
10-22
package com.itgaohe.iodemo.iopractice; import java.io.*; import java.util.Scanner; public class AutoLoad { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String parentDirctory = "src\\userinfo"; String childPath = "cookie.txt"; // 创建文件 create(parentDirctory,childPath); String sourcePath = "src\\userinfo\\test.txt"; String src = "src\\userinfo\\cookie.txt"; // 自动登录 if (autoLoad(src, sourcePath)) { System.out.println("自动登录成功"); } else { System.out.println("用户名或密码错误,登录失败"); // 手动登录 load(scanner,sourcePath,src); } } // 创建文件cookie文件方法 public static void create(String parentDirctory, String childPath) { File parent = new File(parentDirctory); parent.mkdirs(); File cookie = new File(parent, childPath); try { cookie.createNewFile(); } catch (IOException e) { e.printStackTrace(); } if (cookie.exists()) { System.out.println("创建成功"); } else { System.out.println("创建失败"); } } //读取cookie文件件内容 public static String[] readCookie(String src) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(src)); String temp = br.readLine(); String saveName = temp.split("=")[1]; String temp1 = br.readLine(); String savePassword = temp1.split("=")[1]; return new String[]{saveName, savePassword}; } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } // 从userInfo\\test读取信息 public static String[] readTest(String sourcePath) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(sourcePath)); String s = br.readLine(); String[] temp = s.split("&"); String name = temp[0].split("=")[1]; String password = temp[1].split("=")[1]; return new String[]{name, password}; } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } // 手动登录验证 public static void load(Scanner scanner, String sourcePath,String src) { String readdata[] = readTest(sourcePath); if (readdata == null) { System.out.println("系统用户数据文件损坏或不存在"); return; } String trueName = readdata[0]; String truePassword = readdata[1]; System.out.println("欢迎登录系统"); System.out.println("请输入用户名"); String inputName = scanner.nextLine().trim(); System.out.println("请输入密码"); String inputPassword = scanner.nextLine().trim(); if (trueName.equals(inputName) && truePassword.equals(inputPassword)) { System.out.println("登录成功信息已保存,下次可自动登录"); writeDate(src,inputName,inputPassword); } else { System.out.println("登录失败,用户名或密码错误"); } } //自动登录验证方法 public static boolean autoLoad(String src, String sourcePath) { String[] saved = readCookie(src); String[] correct = readTest(sourcePath); if (saved == null || correct == null) { System.out.println("缺少必要信息,无法自动登录。"); return false; } String savedName = saved[0]; String savedPassword = saved[1]; String correctName = correct[0]; String correctPassword = correct[1]; System.out.println("正在尝试自动登录..."); return correctName.equals(savedName) && correctPassword.equals(savedPassword); } // 将信息写入到cookie文件 public static void writeDate(String src,String username,String password){ BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(src)); bw.write("username="+username); bw.newLine(); bw.write("password="+password); bw.newLine(); bw.flush(); System.out.println("文件已保存至:"+src); } catch (IOException e) { e.printStackTrace(); }finally { if (bw != null) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } } 帮我检查和修改一下
10-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值