try {
// db parameters
String url = "jdbc:sqlite: db_path.db";
// create a connection to the database
conn = DriverManager.getConnection(url);
System.out.println("Connection to SQLite has been established.");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
When database is connected a duplicate file is created with same name and size 0Kb inside project folder
String query = "INSERT INTO Path VALUES (?)";
try {
PreparedStatement state = DB_Path.conn.prepareStatement(query);
state.setString(1,path);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
Your connection does not have permissions to access the existing db file. So it creates a new (different) file
The problem is in url of database file i-e space between jdbc:sqlite: and db_path.db
The correct way is:
url = "jdbc:sqlite:db_path.db;
You can use absolute path too but keep in mind the above line of error, for locating some file in directory, it preferable to use backward slash b/w directories because it is used in windows path system and for java syntax it comes two times like this,
url = "jdbc:sqlite:C:\\Desktop\\db_path.db;
or you can use forward slash too,
url = "jdbc:sqlite:C:/Desktop/db_path.db;
I use the following script to create the plugin (gtk), but after I run the script, I do not know where is the created plugin (gtk) file?
I checked the folder
C:\Program Files (x86)\Gatan\DigitalMicrograph\PlugIns
I do not know see any new created gtk file in the folder.
Is this script wrong or the created gtk file should be in somewhere else?
/ Define the necessary variables string base,menu,submenu,item,packageNAME,name
number maxitem,i,j,maxfolder taggroup tgFILES,tgFOLDERS,tg
// Just some starting text in the results window.
result("\n Automatic Installation of all scripts in a folder as Plugin:\n\n")
// First get the default folder. (In this case, the folder last opened within DM)
base = GetApplicationDirectory(2,0)
// Prompt the user with a dialog to choose a folder, with the default folder as first choice.
// If the user cancels the dialog, the script will stop.
If (!GetDirectoryDialog("Please select the folder containing the scripts",base,base)) exit(0)
// Ask the user for a package name
If (!GetString("Name of package file?","",packageNAME)) exit(0)
// Ask the user for a menu name
If (!GetString("Name of menu to install the scripts in","",menu)) exit(0)
// Get all files/folders in the folder as a tag-list
tgFILES = GetFilesInDirectory(base,1)
tgFOLDERS = GetFilesInDirectory(base,2)
// Install all files from the main folder as menu commands.
// Count Items in the folder
maxitem = tgFILES.TagGroupCountTags()
i = 0
// Loop through all items
while (i<maxitem)
{
// get taggroup of item
tgFiles.TagGroupGetIndexedTagAsTagGroup(i,tg)
// get name of file
tg.TagGroupGetTagAsString("Name",item)
// Only if filename end with ".s" continue
If (right(item,2)==".s")
{
// use the name without the ending
name = left(item,len(item)-2)
result("\n Installing: "+item)
// install the menu command
// use the Try-Catch loop to detect problems during install
try
{ AddScriptToPackage(base+item,packageNAME,0,name,menu,"", 0) }
catch
{ result(" \t ERROR DURING INSTALL") } }
i++ }
// Now install all files from sub-folder as sub-menu commands.
// Count subfolders in the folder
maxfolder = tgFOLDERS.TagGroupCountTags()
// Loop for all subfolders
for (j=0;j<maxfolder;j++)
{
// get taggroup of item
tgFolders.TagGroupGetIndexedTagAsTagGroup(j,tg)
// get name of subfolder which is also the name of the submenu
tg.TagGroupGetTagAsString("Name",submenu)
// Get all files in the subfolder as a tag-list
tgFILES = GetFilesInDirectory(base+submenu,1)
// Count Items in the folder
maxitem = tgFILES.TagGroupCountTags()
i = 0
// Loop through all items as before for the main folder
while (i<maxitem)
{
tgFiles.TagGroupGetIndexedTagAsTagGroup(i,tg)
tg.TagGroupGetTagAsString("Name",item)
If (right(item,2)==".s")
{
name = left(item,len(item)-2)
result("\n Installing <"+submenu+">: "+item)
try {
AddScriptToPackage(base+item,packageNAME,0,name,menu,submenu, 0) }
catch
{
result(" \t ERROR DURING INSTALL") } }
i++ } }
You are probably running into the "Compatibility files" feature of Windows 7. GMS 1.x has only one variant of the AddScriptFileToPackage function and it always wants to save the resulting package file to the standard DM PlugIns folder:
C:\Program Files (x86)\Gatan\DigitalMicrograph\Plugins
But in Windows 7, such direct writing of files to subfolders of the Program Files directory is prevented and files are instead written to a user-specific local directory named as follows:
C:\Users\USERNAME\AppData\Local\VirtualStore\Program Files (x86)\Gatan\DigitalMicrograph\Plugins
However, one can easily make such virtualized files visible by clicking on the "Compatibility files" button that appears in the tool bar of the Windows explorer window for the standard PlugIns folder.
This depends a little on the GMS version and the OS version you're running on. I assume you're using GMS 2.x on a Windows7 or Windows8 machine, then the command AddScriptFileToPackage can have two syntax versions:
void AddScriptFileToPackage( String file_path, String packageName, Number packageLevel, String packageLocation, String command_name, String menu_name, String sub_menu_name, Boolean isLibrary )
void AddScriptFileToPackage( String file_path, String packageName, Number packageLevel, String command_name, String menu_name, String sub_menu_name, Boolean isLibrary )
in the first version packageLocation could be user_plugin or plugin. If this parameter is omitted (2nd command version) then user_plugin is assumed.
For user_plugin you will find the created file in:
C:\Users\USERNAME\AppData\Local\Gatan\Plugins
where USERNAME is the current windows user.
For plugin you will find the created file in the 'plugins' subfolder relative to the installation of the 'DigitalMicograph.exe', most likely in:
C:\Program Files\Gatan\Plugins
public void testTakeScreenshot()
{
try{
File fscreenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
System.out.println(fscreenshot.getPath());
File fdest = new File("E:/");
FileUtils.copyFile(fscreenshot,fdest);
System.out.println(fdest.getPath());
}catch(Exception e)
{
e.printStackTrace();
}
}
Generated Output at console :
C:\Users\Bunty\AppData\Local\Temp\screenshot1773089913844817102.png
java.io.IOException: Destination 'E:\' exists but is a directory
The test is running Ok but the file is created as shown in the console. while copying the link . I couldnt find any file for the same. Also the copy function is not working ; hence no file is present in E drive.
As the errormessage suggests, you shouldn't give the path to the directory ('E:\'), but the path to the file. Try:
File fdest = new File("E:/screenshot.png");
I've been trying to read a pre-built file with Car Maintenance tips, there's one in each line of my "Tips.txt" file. I've tried to follow around 4 or 5 different approaches but It's not working, it compiles but I get an exception. Here's what I've got:
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (StreamReader sr = new StreamReader(store.OpenFile("Tips.txt", FileMode.Open, FileAccess.Read)))
{
string line;
while ((line = sr.ReadLine()) != null)
{
(App.Current as App).MyTips.Insert(new DoubleNode(line));
}
}
}
I'm getting this "Operation not permitted on IsolatedStorageFileStream", from the info inside the 2nd using statement. I tried with the build action of my "Tips.txt" set to resource, and content, yet I get the same result.
Thanks in advance.
Since you've added it to your project directory, you can't read it using Isolated Storage methods. There are various ways you can load the file. One way would be to set the text file's build type to Resource, then read it in as a stream:
//Replace 'MyProject' with the name of your XAP/Project
Stream txtStream = Application.GetResourceStream(new Uri("/MyProject;component/myTextFile.txt",
UriKind.Relative)).Stream;
using(StreamReader sr = new StreamReader(txtStream))
{
//your code
}
I am looking to access all files in a local directory in a JBoss application. I can put the directory anywhere in my war including WEB-INF if necessary. I then want to access each file in the directory sequentially. In a normal application if the directory was in the run location I could do something like:
File f = new File("myDir");
if(f.isDirectory && f.list().length != 0)
{
for(String fileName : f.list())
{
//do Read-Only stuff with fileName
}
}
I'm looking for a best-practices solution, so if I'm going about this wrong then please point me to the right way to access an unknown set of resources.
First thing to note: you're only going to get this to work if you have an exploded WAR, or possibly if the servlet container explodes the WAR for you.
With that caveat in mind, you could use ServletContext.getRealPath() as your starting point. You'd need to know the name of at least one file in the webapp's root directory, and go from there:
String knownFilePath = servletContext.getRealPath("knownFile");
File webAppRootDir = new File(knownFilePath).getParentFile();
// and then as per the question
File f = webAppRootDir ;
if(f.isDirectory && f.list().length != 0)
{
for(String fileName : f.list())
{
//do Read-Only stuff with fileName
}
}
Getting hold of ServletContext is left as an exercise for the reader.