String path=this.getClass().getResource("info.txt").toString();
void writeinfo()
{
PrintWriter writer = null;
try {
writer = new PrintWriter(path);
writer.println(highscore);
for(int i=1;i<=30;i++)
{
for(int j=1;j<=30;j++)
{
writer.println(v[i][j]);
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
writer.close();
}
}
i just pasted the relevant part of the code. It says "java.io.FileNotFoundException", but the file does exist! I searched online but i find no solution to this.
Related
something is really messed up. I've got a ".ser" document in the assets folder, which stores an ArrayList of Objetcs. In an android application, I want to read this objects. There are a lot of posts related to this issue, however none of them could solve my problem. The strange part is, when I am using similar code in non - android context / "normal" java, it works properly. Here, the last line throws a NullPointerException - What is going wrong?
public void getData() {
ArrayList<MyClass> output= null;
InputStream is = null;
ObjectInputStream ois = null;
try{
is = this.getAssets().open("data.ser");
ois = new ObjectInputStream(is);
output = (ArrayList<MyClass>)ois.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.d("TAG", output.get(0).getId());
}
I would create a class and place the array within a single object:
public class ListObjects implements Serializable {
List<MyClass> listMyClass = new ArrayList<>();
public ListObjects(){
}
public List<MyClass> getListMyClass() {
return listMyClass;
}
public void setListMyClass(List<MyClass> listMyClass) {
this.listMyClass = listMyClass;
}
}
I had a similar problem. And it was because the name of the package in the java app was not called the same as the package name in android. And therefore I did not recognize them as equal objects. This is how I do it:
public static Object fromData(byte[] data) {
ObjectInputStream ois = null;
Object object = null;
try {
ois = new ObjectInputStream(
new ByteArrayInputStream(data));
object = ois.readObject();
} catch (Exception ex) {
Logger.getLogger(ModeloApp.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
ois.close();
} catch (Exception ex) {
Logger.getLogger(ModeloApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
return object;
}
The code below is supposed to create and write to a file, but it doesn't create a file in my directory. Everything with the Scanner is working, it scans everything from jTextField perfectly.
OutputStream os;
try {
os = new FileOutputStream("kurinys.dat");
try (DataOutputStream dos = new DataOutputStream(os)) {
Scanner skanuoklisSaugojimui = new Scanner(jTextField1.getText());
while(skanuoklisSaugojimui.hasNextInt()){
int natosAukstis = skanuoklisSaugojimui.nextInt();
dos.writeInt(natosAukstis);
}
}
os.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Grafika.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Grafika.class.getName()).log(Level.SEVERE, null, ex);
}
Can someone please help me find the problem?
Don't use a nested try, it doesnt have any sense and could case a lot of problems in terms of exception handling.
OutputStream os;
try {
os = new FileOutputStream("kurinys.dat");
}
catch (FileNotFoundException e) {
}
try (DataOutputStream dos = new DataOutputStream(os)) {
Scanner skanuoklisSaugojimui = new Scanner(jTextField1.getText());
while(skanuoklisSaugojimui.hasNextInt()){
int natosAukstis = skanuoklisSaugojimui.nextInt();
dos.writeInt(natosAukstis);
}
}
os.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Grafika.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Grafika.class.getName()).log(Level.SEVERE, null, ex);
}
I'm trying to close a RandomAccessFile but resource remain busy.
Code:
public boolean isOpen(RandomAccessFile f) {
try {
f.length() ;
return true ;
}
catch (IOException e) {
return false ;
}
}
this.rfmFile = new File(filePath);
try {
this.rfmRandomAccessFile = new RandomAccessFile(rfmFile, "rws");
} catch(Exception e){
}finally{
this.rfmRandomAccessFile.close();
}
while(!isOpen(this.rfmRandomAccessFile));
log.debug("I Finally Closed this RAF");
Log is not showed and thread goes in loop.
When I try to access to my resource from shell it gives me "Device or Resource busy".
The only way to access is kill java process.
When you are trying to access the RandomAccessFile length(), method, it is already closed and thus you cannot access it anymore.
You probably want to use the length() method of File. Your loop cannot work as the RandomAccessFile was already closed.
But I must admit I am clueless on the low level reason why rfmRandomAccessFile would not really be closed. It could be a side effect of your strange loop trying to get the size of a closed file.
[edit:]Could not reproduce your issue with the following piece of code:
package com.company;
import java.io.*;
public class Main {
public static void main(String[] args) {
File file = new File("foobar.txt");
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file, "rws");
randomAccessFile.write(new byte[]{'f'});
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(randomAccessFile !=null){
try {
randomAccessFile.close();
} catch (IOException e) {
//doh!
}
}
}
FileReader reader = null;
try {
reader = new FileReader(file);
char read = (char) reader.read();
System.out.println("what was written: "+read);
System.out.println("file size: "+file.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(reader !=null){
try {
reader.close();
} catch (IOException e) {
//doh!
}
}
}
}
}
I am having a hard time with saving my file to my C: drive using my save button. My action listener looks like this
saveButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
File savedFile = new File("C:\\data\\inventory.dat");
if (savedFile.exists() == false)
{
try
{
savedFile.createNewFile();
outputText.append("The file has been saved\n");
}
catch (IOException ex)
{
Logger.getLogger(JavaGUIFixed.class.getName()).log(Level.SEVERE, null, ex);
}
}
else
{
if (savedFile.exists() == true)
{
try
{
savedFile.createNewFile();
outputText.append("The file already exists\n and has been overwritten\n");
}
catch (IOException ex)
{
Logger.getLogger(JavaGUIFixed.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
);
The problem I keep running into is
Dec 06, 2013 12:14:16 AM javaguifixed.JavaGUIFixed$8 actionPerformed
SEVERE: null
java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:1006)
Can anyone point me in the right direction? Why is this not saving? Essentially it should be creating the directory as well as the file if it doesn't exist and it should overwrite it if it does.
I did some playing around and got this to work. Below is my code
saveButton.addActionListener(
new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
File savedFile = new File("C:\\Users\\kerinr\\Documents\\data\\inventory.dat");
if (savedFile.exists() == false) {
try {
savedFile.createNewFile();
outputText.append("The file has been saved\n");
} catch (IOException ex) {
Logger.getLogger(JavaGUIFixed.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
if (savedFile.exists() == true) {
try {
savedFile.createNewFile();
outputText.append("The file already exists\n and has been overwritten\n");
} catch (IOException ex) {
Logger.getLogger(JavaGUIFixed.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Writer writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("C:\\Users\\kerinr\\Documents\\data\\inventory.dat"), "utf-8"));
for (int i = 0; i < inventory.size(); i++) {
writer.write(inventory.get(i).itemName + " \r\n");
writer.write(inventory.get(i).inStock + " \r\n");
writer.write(inventory.get(i).itemNumber + " \r\n");
writer.write(inventory.get(i).unitPrice + " \r\n");
writer.write(inventory.get(i).restockingFee + " \r\n");
writer.write(inventory.get(i).inventoryValue + " \r\n");
}
} catch (IOException ex) {
// report
} finally {
try {
writer.close();
} catch (Exception ex) {
}
}
}
});
One thing I have noticed though is that the folder "data" has to already be in place. If it is not then it will not save the file.
I'm having a problem here
I am using netbeans!
I am unable to use the .write
Here are my codes:
ModFile=new File(NameText.getText() + ".mod");
if(!ModFile.exists()){
try {
ModFile.createNewFile();
System.out.println("Mod file has been created to the current directory");
ModFile.*write*(CodesBox.getText());
} catch (IOException ex) {
Logger.getLogger(ModMakerGui.class.getName()).log(Level.SEVERE, null, ex);
}
}
This line:
ModFile.*write*(CodesBox.getText());
is giving me problems!
Please help me here
File does not have write method. Please use FileWriter or Print Writer
ModFile=new File(NameText.getText() + ".mod");
FileWriter writer=null;
if(!ModFile.exists()){
try {
ModFile.createNewFile();
writer=new FileWriter(ModFile);
System.out.println("Mod file has been created to the current directory");
writer.write(CodesBox.getText());
} catch (IOException ex) {
Logger.getLogger(ModMakerGui.class.getName()).log(Level.SEVERE, null, ex);
}
}