how write txt file in File Input Stream without all path - java

Properties connectDB = new Properties();
FileInputStream info = new FileInputStream("C:\codingUQU\netbeansCode\appliancesProject\jdbcConnect.txt");
connectDB.load(info);
this my code can i write a txt file without all path ?
i try some method nothing work

Related

Program works fine in compiler but not in jar (Java)

I Exported my program into a jar file but when I ran it, it seems as if the .properties file isn't found by the program. I made sure it was in the jar file & it worked fine prior to exporting it. I read something about using getClass().getResourceAsStream() instead of FileInputStream and FileOutputStream but can't seem to understand how that would help. Any ideas? These are the two methods that use the file.
private void UpdateData() throws IOException{
FileInputStream in = new FileInputStream("config.properties");
Properties props = new Properties();
props.load(in);
in.close();
FileOutputStream out = new FileOutputStream("config.properties");
props.setProperty("prop1", prop1TextArea.getText().toString());
props.setProperty("prop2", prop2TextArea.getText().toString());
props.setProperty("prop3", prop3TextArea.getText().toString());
props.store(out, null);
out.close();
}
private void setText() throws IOException {
FileInputStream in = new FileInputStream("config.properties");
Properties props = new Properties();
props.load(in);
in.close();
FileOutputStream out = new FileOutputStream("config.properties");
prop1TextArea.setText(props.getProperty("prop1"));
prop2TextArea.setText(props.getProperty("prop2"));
prop3TextArea.setText(props.getProperty("prop3"));
out.close();
}
To access resources in your .jar file you have to access them as resources not as files.
The .properties are not in the filesystem anymore and therefore you cannot access them via FileInputStream.
If you want to save them afterwards you have to create new .properties on the first start of your program. So you have to check first if the file exists (e.g. via File.exists()) and either work without the properties from the file or create a new file and use this then.

Java Android Property file

I am trying to load some data saved in my property file in an Android Application.
I have put my property file under the src folder. Every time I try to load data from my file it keeps telling me FileNotFoundException open failed ENOENT (No such file or directory).
My Code is as follows:
This code is to save the file (new create)
File file = new File("src/com/example/testphonegap/SilverAngel.properties");
FileOutputStream fileOut = new FileOutputStream(file);
properties.store(fileOut, "Properties");
fileOut.close();
This code is to load data
properties = new Properties();
InputStream is = null;
// First try loading from the current directory
try {
File f = new File("src/com/example/testphonegap/SilverAngel.properties");
is = new FileInputStream(f);
// Try loading properties from the file (if found)
properties.load(is);
GetPersonaliseSettings();
GetUserSettings();
GetFavSettings();
}
catch ( Exception e ) {
is = null;
}
Can you tell me what I am doing wrong please? Is it where the file is saved or am I missing something in my code?
It's an Android Application, so the file is stored on the Android device and not on your computer.
If you want to save the file on the SD card you can write the following:
String root = Environment.getExternalStorageDirectory().toString();
File dir = new File(root + "/subfolderForYourApp");
dir.mkdirs();
This will create a subfolder for your application on the SD card.
To create a new file in this directory:
File file = new File(dir, "SilverAngel.properties");
FileOutputStream fileOut = new FileOutputStream(file);
properties.store(fileOut, "Properties");
fileOut.close();
Don't forget to add the permission to the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
try this
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("com/example/testphonegap/SilverAngel.properties");
properties.load(inputStream);
make sure you don't add "src" before property file name.
Put it in the assets folder and load it from there.
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("SilverAngel.properties");
properties.load(inputStream);

Assigning a destination for properties file in a java program

Sorry if this seems like a newbie question and im sure its just a little thing i need to change but it seems like my program cannot locate the destination for a properties file i coded in.
here is my code
public String metrics() throws IOException {
String result = "";
Properties prop = new Properties();
String propFileName = "C:\\Users\\JChoi\\Desktop\\config.properties";
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
prop.load(inputStream);
if (inputStream == null) {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
// get the property value and print it out
String Metrics = prop.getProperty("Metrics");
result = Metrics;
System.out.println(result);
return result;
}
I get a nullpointerexception error everytime i run the code but however, when i put the properties file in the resources folder and edit the string name to...
String propFileName = "config.properties";
works fine...any suggestions?
EDIT:
String result = "";
Properties prop = new Properties();
String propFileName = "C:\\Users\\JChoi\\Desktop\\config.properties";
FileInputStream fileInputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
prop.load(fileInputStream);
SOLVED!
String propFileName = "C:\\Users\\JChoi\\Desktop\\googlebatchfile\\config.properties";
BufferedInputStream inputStream;
FileInputStream fileInputStream = new FileInputStream(propFileName);
inputStream = new BufferedInputStream(fileInputStream);
If you know the full path to a file, then do not try to open it using a classpath search (which is what getResourceAsStream() does).
Instead open the file using an inputsteam that takes a path.
Here is some code:
FileInputStream inputStream = new FileInputStream(propFileName);
The following might be a better technique (I'm not sure with property loading):
BufferedInputStream inputStream;
FileInputStream fileInputStream = new FileInputStream(propFileName);
inputStream = new BufferedInputStream(fileInputStream);
You are attempting to load a file using a classpath-based input stream but specifying a filepath.
This:
getClass().getClassLoader().getResourceAsStream(propFileName);
Will attempt to search the classpath starting at the root (based on whatever the classloader considers the root).
If you want to load a file from outside the classpath, you probably just want to use something like a FileInputStream instead.

Java Properties.store is deleting other entries

I am trying to modify a config file in Java using Properties. I try to modify two of the multiple entries like this:
Properties properties = new Properties();
FileInputStream fin = null;
FileOutputStream fout = null;
fin = new FileInputStream(mCallback.getConfFile());
fout = new FileOutputStream(mCallback.getConfFile());
properties.load(fin);
properties.setProperty(Wrapper.GAME_PATH_KEY, (String)gamePathText.getText());
properties.setProperty(Wrapper.GAME_TYPE_KEY, (String)selectedGame.getSelectedItem());
properties.store(fout, null);
But when I check the file after the result I find out that the whole file was overwritten, leaving only these two entries. This is an android app though I guess it's not relevant to the problem here. What I am doing wrong?
You have to read all properties and then modify the ones you want. After that you have to write all to file. You cannot do only an item modification. The Properties API doesn't provide that functionality to modify.
Edit:
Interchange these two statements-
fout = new FileOutputStream(mCallback.getConfFile());
properties.load(fin);
You should load first before you create a file with the same name.
From Properties:
public void store(OutputStream out,
String comments)
throws IOException
Writes this property list (key and element pairs) in this Properties table to the output > stream in a
format suitable for loading into a Properties table using the
load(InputStream) method.
Properties from the defaults table of this
Properties table (if any) are not written out by this method.
This method outputs the comments, properties keys and values in the
same format as specified in store(Writer), with the following
differences:
So, Load the data first, then set the required data, then store it.
Properties prop =new Properties();
prop.load(new FileInputStream(filename));
prop.setProperty(key, value);
prop.store(new FileOutputStream(filename),null);
The previous poster was kind of right, just not at the right place.
You need to open the FileOutputStream after you've loaded the properties otherwise it clears the file's content.
Properties properties = new Properties();
FileInputStream fin = null;
FileOutputStream fout = null;
fin = new FileInputStream(mCallback.getConfFile());
// if fout was here, the file would be cleared and reading from it would produce no properties
properties.load(fin);
properties.setProperty(Wrapper.GAME_PATH_KEY, (String)gamePathText.getText());
properties.setProperty(Wrapper.GAME_TYPE_KEY, (String)selectedGame.getSelectedItem());
fout = new FileOutputStream(mCallback.getConfFile());
properties.store(fout, null);

Using a PrintWriter and File Object to Write to an Output File

I have a JFileChooser object used to get a data file from the user. What I need to do is create a File object and PrintWriter object so that I can write to a file named "output.txt". The file should be written to the same directory from which the data file was retrieved from.
So far I have tried:
// Write to a text file`
File file = new File ("output.txt");
PrintWriter printWriter = new PrintWriter (f);
This snippet of code creates the output file, but I need to it be written to the same directory from which the data file came from.
First thoughts were to call the .getPath() method (see below) on the JFileChooser object.
String fileDir = inputFile.getPath();
String fileName = "output.txt";
File f = new File (fileDir + "/" + fileName);
PrintWriter printWriter = new PrintWriter (f);
Thoughts?
inputFile.getPath() will get you the file path. You need inputFile.getParent() which will get you the directory of the file.
String fileDir = inputFile.getParent();
String fileName = "output.txt";
File f = new File (fileDir,fileName);
PrintWriter printWriter = new PrintWriter (f);

Categories

Resources