filePath = saveFileName.getAbsolutePath().toString().trim();//获取文件的路径+文件名
比如我选择D盘下的123.txt文件
经过正则表达式替换后 filePath = filePath.replaceAll("\\\\", "\\\\\\\\");
此时的filePath为D:\\123.txt
读取的时候 RandomAccessFile fileOut = new RandomAccessFile(filePath,"r");会报错
但是如果这样写RandomAccessFile fileOut = new RandomAccessFile("D:\\123.txt","r");就没事
不知道为什么
错误信息如下:
java.io.FileNotFoundException: (Permission denied)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(Unknown Source)
at java.io.RandomAccessFile.<init>(Unknown Source)
at ChatSwing$ActListener.actionPerformed(ChatSwing.java:312)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at com.jtattoo.plaf.BaseButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
请高手指正
13 个解决方案
#1
\\是外部表示,实际值是\
概念错误了
概念错误了
#2
filePath = filePath.replaceAll("\\\\", "\\\\\\\\");
我是将单斜杠替换成了双斜杠
这样出来的值确实是我需要的没错 是D:\\123.txt
为什么不对呢
我是将单斜杠替换成了双斜杠
这样出来的值确实是我需要的没错 是D:\\123.txt
为什么不对呢
#3
saveFileName.getSelectedFile();这样就能获得路径了,
#4
实际上是/
#5
把代码贴完整点,
#6
//选择文件
if(e.getSource() == selectFileBtn) {
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showDialog(null, "请选择要发送的文件");
if(returnVal == JFileChooser.APPROVE_OPTION) {
saveFileName=chooser.getSelectedFile();//获取选择的文件
fileName = saveFileName.getName().toString().trim();//获取文件名
filePath = saveFileName.getAbsolutePath().toString().trim();//获取文件的路径
}
filePath = filePath.replaceAll("\\\\", "\\\\\\\\");
}
//发送文件
if(e.getSource() == sendFileBtn) {
try {
filePath = filePath.replaceAll("\\\\", "\\\\\\\\");
RandomAccessFile fileOut = new RandomAccessFile(filePath,"r");我说的错误就在这里 错误信息如上所示
File file = new File(filePath);
long num = file.length()/buffer.length;
for(int i = 0;i < num+1;i++) {//读满一个包就发送,知道文件内容读取完毕
byte[] buf = new byte[16*1024];
fileOut.read(buf, 0, buf.length);
DatagramPacket p = new DatagramPacket(buf,buf.length
,InetAddress.getByName(friendIpText.getText())
,Integer.valueOf(friendPortText.getText()));
p.setData(buf);
DatagramSocket sockFile = new DatagramSocket();
sockFile.send(p);//向指定IP及端口发送包
sockFile.close();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (NumberFormatException e1) {
e1.printStackTrace();
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
#7
上面我发错了 发送文件里没有这一行 filePath = filePath.replaceAll("\\\\", "\\\\\\\\");
在选择文件中已经有了
在选择文件中已经有了
#8
是这样的
//选择文件
if(e.getSource() == selectFileBtn) {
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showDialog(null, "请选择要发送的文件");
if(returnVal == JFileChooser.APPROVE_OPTION) {
saveFileName=chooser.getSelectedFile();//获取选择的文件
fileName = saveFileName.getName().toString().trim();//获取文件名
filePath = saveFileName.getAbsolutePath().toString().trim();//获取文件的路径
}
filePath = filePath.replaceAll("\\\\", "\\\\\\\\");
}
//发送文件
if(e.getSource() == sendFileBtn) {
try {
RandomAccessFile fileOut = new RandomAccessFile(filePath,"r");//创建从中读取的随机文件流
File file = new File(filePath);
long num = file.length()/buffer.length;
for(int i = 0;i < num+1;i++) {//读满一个包就发送,知道文件内容读取完毕
byte[] buf = new byte[16*1024];
fileOut.read(buf, 0, buf.length);
DatagramPacket p = new DatagramPacket(buf,buf.length
,InetAddress.getByName(friendIpText.getText())
,Integer.valueOf(friendPortText.getText()));
p.setData(buf);
DatagramSocket sockFile = new DatagramSocket();
sockFile.send(p);//向指定IP及端口发送包
sockFile.close();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (NumberFormatException e1) {
e1.printStackTrace();
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
#9
String filePath = "";
if(e.getSource() == selectFileBtn) {
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showDialog(null, "请选择要发送的文件");
if(returnVal == JFileChooser.APPROVE_OPTION) {
filePath = chooser.getSelectedFile().toString();//获取选择的文件
}
}
//发送文件
if(e.getSource() == sendFileBtn) {
try {
RandomAccessFile fileOut = new RandomAccessFile(filePath,"r");//创建从中读取的随机文件流
File file = new File(filePath);
long num = file.length()/buffer.length;
for(int i = 0;i < num+1;i++) {//读满一个包就发送,知道文件内容读取完毕
byte[] buf = new byte[16*1024];
fileOut.read(buf, 0, buf.length);
DatagramPacket p = new DatagramPacket(buf,buf.length
,InetAddress.getByName(friendIpText.getText())
,Integer.valueOf(friendPortText.getText()));
p.setData(buf);
DatagramSocket sockFile = new DatagramSocket();
sockFile.send(p);//向指定IP及端口发送包
sockFile.close();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (NumberFormatException e1) {
e1.printStackTrace();
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
#10
String filePath = chooser.getSelectedFile().toString();//获取被选择文件的路径,我测试了没有问题的,
#11
还是这个错误:
java.io.FileNotFoundException: (Permission denied)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(Unknown Source)
at java.io.RandomAccessFile.<init>(Unknown Source)
at ChatSwing$ActListener.actionPerformed(ChatSwing.java:311)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at com.jtattoo.plaf.BaseButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
#12
谁能解释一下 Permission denied 这是什么错误
#13
RandomAccessFile fileOut = new RandomAccessFile(filePath,"r");
我说的错误就在这里 错误信息如上所示
File file = new File(filePath);
你应该把这2个语句倒过来写,因为你不能给randomaccessfile传送一个filepath参数,应该传送file参数
File file = new File(filePath);
你应该把这2个语句倒过来写,因为你不能给randomaccessfile传送一个filepath参数,应该传送file参数
#1
\\是外部表示,实际值是\
概念错误了
概念错误了
#2
filePath = filePath.replaceAll("\\\\", "\\\\\\\\");
我是将单斜杠替换成了双斜杠
这样出来的值确实是我需要的没错 是D:\\123.txt
为什么不对呢
我是将单斜杠替换成了双斜杠
这样出来的值确实是我需要的没错 是D:\\123.txt
为什么不对呢
#3
saveFileName.getSelectedFile();这样就能获得路径了,
#4
实际上是/
#5
把代码贴完整点,
#6
//选择文件
if(e.getSource() == selectFileBtn) {
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showDialog(null, "请选择要发送的文件");
if(returnVal == JFileChooser.APPROVE_OPTION) {
saveFileName=chooser.getSelectedFile();//获取选择的文件
fileName = saveFileName.getName().toString().trim();//获取文件名
filePath = saveFileName.getAbsolutePath().toString().trim();//获取文件的路径
}
filePath = filePath.replaceAll("\\\\", "\\\\\\\\");
}
//发送文件
if(e.getSource() == sendFileBtn) {
try {
filePath = filePath.replaceAll("\\\\", "\\\\\\\\");
RandomAccessFile fileOut = new RandomAccessFile(filePath,"r");我说的错误就在这里 错误信息如上所示
File file = new File(filePath);
long num = file.length()/buffer.length;
for(int i = 0;i < num+1;i++) {//读满一个包就发送,知道文件内容读取完毕
byte[] buf = new byte[16*1024];
fileOut.read(buf, 0, buf.length);
DatagramPacket p = new DatagramPacket(buf,buf.length
,InetAddress.getByName(friendIpText.getText())
,Integer.valueOf(friendPortText.getText()));
p.setData(buf);
DatagramSocket sockFile = new DatagramSocket();
sockFile.send(p);//向指定IP及端口发送包
sockFile.close();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (NumberFormatException e1) {
e1.printStackTrace();
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
#7
上面我发错了 发送文件里没有这一行 filePath = filePath.replaceAll("\\\\", "\\\\\\\\");
在选择文件中已经有了
在选择文件中已经有了
#8
是这样的
//选择文件
if(e.getSource() == selectFileBtn) {
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showDialog(null, "请选择要发送的文件");
if(returnVal == JFileChooser.APPROVE_OPTION) {
saveFileName=chooser.getSelectedFile();//获取选择的文件
fileName = saveFileName.getName().toString().trim();//获取文件名
filePath = saveFileName.getAbsolutePath().toString().trim();//获取文件的路径
}
filePath = filePath.replaceAll("\\\\", "\\\\\\\\");
}
//发送文件
if(e.getSource() == sendFileBtn) {
try {
RandomAccessFile fileOut = new RandomAccessFile(filePath,"r");//创建从中读取的随机文件流
File file = new File(filePath);
long num = file.length()/buffer.length;
for(int i = 0;i < num+1;i++) {//读满一个包就发送,知道文件内容读取完毕
byte[] buf = new byte[16*1024];
fileOut.read(buf, 0, buf.length);
DatagramPacket p = new DatagramPacket(buf,buf.length
,InetAddress.getByName(friendIpText.getText())
,Integer.valueOf(friendPortText.getText()));
p.setData(buf);
DatagramSocket sockFile = new DatagramSocket();
sockFile.send(p);//向指定IP及端口发送包
sockFile.close();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (NumberFormatException e1) {
e1.printStackTrace();
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
#9
String filePath = "";
if(e.getSource() == selectFileBtn) {
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showDialog(null, "请选择要发送的文件");
if(returnVal == JFileChooser.APPROVE_OPTION) {
filePath = chooser.getSelectedFile().toString();//获取选择的文件
}
}
//发送文件
if(e.getSource() == sendFileBtn) {
try {
RandomAccessFile fileOut = new RandomAccessFile(filePath,"r");//创建从中读取的随机文件流
File file = new File(filePath);
long num = file.length()/buffer.length;
for(int i = 0;i < num+1;i++) {//读满一个包就发送,知道文件内容读取完毕
byte[] buf = new byte[16*1024];
fileOut.read(buf, 0, buf.length);
DatagramPacket p = new DatagramPacket(buf,buf.length
,InetAddress.getByName(friendIpText.getText())
,Integer.valueOf(friendPortText.getText()));
p.setData(buf);
DatagramSocket sockFile = new DatagramSocket();
sockFile.send(p);//向指定IP及端口发送包
sockFile.close();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (NumberFormatException e1) {
e1.printStackTrace();
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
#10
String filePath = chooser.getSelectedFile().toString();//获取被选择文件的路径,我测试了没有问题的,
#11
还是这个错误:
java.io.FileNotFoundException: (Permission denied)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(Unknown Source)
at java.io.RandomAccessFile.<init>(Unknown Source)
at ChatSwing$ActListener.actionPerformed(ChatSwing.java:311)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at com.jtattoo.plaf.BaseButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
#12
谁能解释一下 Permission denied 这是什么错误
#13
RandomAccessFile fileOut = new RandomAccessFile(filePath,"r");
我说的错误就在这里 错误信息如上所示
File file = new File(filePath);
你应该把这2个语句倒过来写,因为你不能给randomaccessfile传送一个filepath参数,应该传送file参数
File file = new File(filePath);
你应该把这2个语句倒过来写,因为你不能给randomaccessfile传送一个filepath参数,应该传送file参数