Why does the run file opens and closes automatically after I run a file on netbeans? - netbeans

I have a table that has details and a save button. When I click on the save button it is supposed to save to a text file. It does get saved but the issue is the run file opens and closes automatically when I pressed the save button. Here is the code
private void ButtonExportActionPerformed(java.awt.event.ActionEvent evt) {
//saving file
String filePath = "C:\\Users\\User\\OneDrive\\Desktop\\Second Year First Semester\\Data Structures and Algorithms\\Assignment\\assignment.txt";
File file = new File(filePath);
String name1 = Member1Txt.getText();
String name2 = Member2Txt.getText();
String name3 = Member3Txt.getText();
String name4 = Member4Txt.getText();
String name5 = Member5Txt.getText();
try {
FileWriter fw = new FileWriter(file,true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(""+ name1);
bw.write(System.getProperty("line.separator"));
bw.write(""+ name2);
bw.write(System.getProperty("line.separator"));
bw.write(""+ name3);
bw.write(System.getProperty("line.separator"));
bw.write(""+ name4);
bw.write(System.getProperty("line.separator"));
bw.write(""+ name5);
bw.write(System.getProperty("line.separator"));
bw.close();
fw.close();
JOptionPane.showMessageDialog(null, "Export Successful");
setVisible(false);
new DirectoryListingSystem().setVisible(true);
} catch (IOException ex) {
}
}
I want the run file to not close when I press on the save button. Can someone tell me what I should do?

Related

MalformedInputException: Input length = 1 while reading text file with Files.readAllLines(Path.get("file").get(0);

Why am I getting this error? I'm trying to extract information from a bank statement PDF and tally different bills for the month. I write the data from a PDF to a text file so I can get specific data from the file (e.g. ASPEN HOME IMPRO, then iterate down to what the dollar amount is, then read that text line to a string)
When the Files.readAllLines(Path.get("bankData").get(0) code is run, I get the error. Any thoughts why? Encoding issue?
Here is the code:
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\wmsai\\Desktop\\BankStatement.pdf");
PDFTextStripper stripper = new PDFTextStripper();
BufferedWriter bw = new BufferedWriter(new FileWriter("bankData"));
BufferedReader br = new BufferedReader(new FileReader("bankData"));
String pdfText = stripper.getText(Loader.loadPDF(file)).toUpperCase();
bw.write(pdfText);
bw.flush();
bw.close();
LineNumberReader lineNum = new LineNumberReader(new FileReader("bankData"));
String aspenHomeImpro = "PAYMENT: ACH: ASPEN HOME IMPRO";
String line;
while ((line = lineNum.readLine()) != null) {
if (line.contains(aspenHomeImpro)) {
int lineNumber = lineNum.getLineNumber();
int newLineNumber = lineNumber + 4;
String aspenData = Files.readAllLines(Paths.get("bankData")).get(0); //This is the code with the error
System.out.println(newLineNumber);
break;
} else if (!line.contains(aspenHomeImpro)) {
continue;
}
}
}
So I figured it out. I had to check the properties of the text file in question (I'm using Eclipse) to figure out what the actual encoding of the text file was.
Then, when creating the file in the program, encode the text file to UTF-8 so that Files.readAllLines could read and grab the data I wanted to get.

I get a file in use error. If I double click on the ISO file from explorer it does open

Hi folks I am new to C# using the following code I get a file in use error. If I double click on the ISO file from explorer it does open. Not sure what I am missing here if I can manually open the ISO why does my code not open it?
code:
private void btnOpenISO_Click(object sender, EventArgs e)
{
listBox2.Items.Clear();
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = Path.GetPathRoot(Environment.SystemDirectory);
openFileDialog1.Filter = "ISO files (*.iso)|*.iso|All files (*.*)|*.*";
openFileDialog1.Multiselect = false;
openFileDialog1.FilterIndex = 1;
openFileDialog1.ShowDialog();
using (FileStream fs = File.Open(openFileDialog1.FileName, FileMode.Open))
{
CDReader cd = new CDReader(fs, true, true);
foreach (var dir in cd.Root.GetDirectories())
{
listBox2.Items.Add(dir.Name);
// Console.WriteLine(dir.Name);
}
}
}

file not downloading properly

I am downloading a file from a url and saving it to a directory on my phone.
the path is: /private/var/mobile/Applications/17E4F0B0-0781-4259-B39D-37057D44B778/Documents/samplefile.txt
However, when i debug the file is created and downloaded. But, when i ad-hoc it and run the file. samplefile.txt is created but it's blank.
Code:
String directory = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
var filename = Path.Combine (directory, "samplefile.txt");
if (!File.Exists (filename)) {
File.Create (filename);
var webClient = new WebClient ();
webClient.DownloadStringCompleted += (s, e) => {
var text = e.Result; // get the downloaded text
File.WriteAllText (filename, text);
};
var url = new Uri (/**myURL**/);
webClient.Encoding = Encoding.UTF8;
webClient.DownloadStringAsync (url);
I modified your sample slightly and the following works for me.
The StreamReader is only there just to re-read in the contents of the file to confirm that its the same contents in the file as that of the downloaded file:-
If you put a breakpoint there also you can manually inspect same contents as downloaded.
string directory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var filename = Path.Combine(directory, "samplefile.txt");
if (!File.Exists(filename))
{
var webClient = new WebClient();
webClient.DownloadStringCompleted += (s, e) =>
{
// Write contents of downloaded file to device:-
var text = e.Result; // get the downloaded text
StreamWriter sw = new StreamWriter(filename);
sw.Write(text);
sw.Flush();
sw.Close();
sw = null;
// Read in contents from device and validate same as downloaded:-
StreamReader sr = new StreamReader(filename);
string strFileContentsOnDevice = sr.ReadToEnd();
System.Diagnostics.Debug.Assert(strFileContentsOnDevice == text);
};
var url = new Uri("**url here**, UriKind.Absolute);
webClient.Encoding = Encoding.UTF8;
webClient.DownloadStringAsync(url);
}

How to get a text file read line by line?

I am creating an application which is text file reader but It reads the text file content line by line and then if a reserved word is red a function will execute.
For example I have a text file that contains the reserve word [PlaySound]+ sound file on its first line, when the reader reads the line a function will execute and plays the music file that is with the reserve word.
So is it possible to create this? and if it is how can i make the line reader?
File file = new File("inputFile.txt");
Scanner sc = null;
try {
sc = new Scanner(file);
String line = null;
while (sc.hasNextLine()) {
String soundFile = null;
line = sc.nextLine();
if (line.indexOf("[PlaySound]") >= 0) {
soundFile = // some regex to extract the sound file from the line
playSoundFile(soundFile);
}
}
sc.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
finally {
if (sc != null) { sc.close(); }
}

Create doc file from template and adding data from database using open xml

I have a word template and I want to create doc file from that, also I want to replace add data in place of bookmarks present in the template.
I have been able to create a doc file, but I am not able to understand, how to add data in place of bookmarks?
My code till now:
private void CreateSampleWordDocument()
{
string sourceFile = Path.Combine(Environment.CurrentDirectory, "GeneralWelcomeLetter.dotx");
string destinationFile = Path.Combine(Environment.CurrentDirectory, "Sample.docx");
try
{
File.Copy(sourceFile, destinationFile, true);
WordprocessingDocument document = WordprocessingDocument.Open(destinationFile, true);
document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
MainDocumentPart mainPart = document.MainDocumentPart;
DocumentSettingsPart documentSettingPart1 = mainPart.DocumentSettingsPart;
AttachedTemplate attachedTemplate1 = new AttachedTemplate() { Id = "relationId1" };
documentSettingPart1.Settings.Append(attachedTemplate1);
}
catch
{
}
}
Now to add data from database in place of bookmarks?