我正在读取通过套接字发送的文件,然后尝试通过换行符 (\n) 将其拆分,当我读取文件时,我使用的是 byte[] 并将字节数组转换为字符串,以便我可以拆分它。
public String getUserFileData()
{
try
{
byte[] mybytearray = new byte[1024];
InputStream is = clientSocket.getInputStream();
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
is.close();
return new String(mybytearray);
}
catch(IOException e)
{
}
return "";
}
这是用于尝试拆分字符串的代码
public void readUserFile(String userData, Log logger)
{
String[] data;
String companyName;
data = userData.split("\n");
username = data[0];
password = data[1].toCharArray();
companyName = data[2];
quota = Float.parseFloat(data[3]);
company = new Company();
company.readCompanyFile("C:\\Users\\Chris\\Documents\\NetBeansProjects\\ArFile\\ArFile Clients\\" + companyName + "\\"
+ companyName + ".cmp");
cloudFiles = new CloudFiles();
cloudFiles.readCloudFiles(this, logger);
}
它会导致此错误
Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException