How to send data java and html - java

code JAVA
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class test{
public static void main (String[] args)throws Exception{
demoReadall();
}
public static void demoReadall(){
String dates;
String res;
String status;
try{
FileInputStream fstream = new FileInputStream("RunningSystem.log");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] LineArray = strLine.split("\\|");
if("BATCH_PAYMENT".equals(LineArray[1])){
dates = LineArray[2];
res = LineArray[3];
status = LineArray[4];
System.out.println(dates);
System.out.println (res);
System.out.println (status);
}
}
fstream.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
How do you write the front-end code to retrieve information about this code? Please bother me.
I would like to know how to send java and html data, please advise writing and sending data.

Read about .jsp, servlets or Spring framework ;-)

Related

URLConnectionReader produces UnknownHostException

Thanks in advance for every input!
I'm getting a little familiar with how to read data from websites with Java and have tried to do this by reading data using a URLConnectionReader.
Unfortunately I get an UnknownHostException when I test the whole thing in a Java online compiler (https://www.jdoodle.com/online-java-compiler/).
Have I forgotten any imports? I proceeded according to a tutorial.
Code: (designed for online-java-compiler jdoodle):
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public static void main(String[] args)
{
String output = getUrlContents("https://www.tradegate.de/orderbuch_umsaetze.php?isin=NO0010892359");
System.out.println(output);
}
private static String getUrlContents(String theUrl)
{
StringBuilder content = new StringBuilder();
try
{
URL url = new URL(theUrl);
URLConnection urlConnection = url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null)
{
content.append(line + "\n");
}
bufferedReader.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return content.toString();
}
}
Error message:
java.net.UnknownHostException: www.tradegate.de
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
at java.base/java.net.Socket.connect(Socket.java:591)
at java.base/sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:285)
at java.base/sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:182)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569)
at java.base/sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:265)
at java.base/sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:372)
at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1187)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1081)
at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1587)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250)
at URLConnectionReader.getUrlContents(URLConnectionReader.java:21)
at URLConnectionReader.main(URLConnectionReader.java:8)
I separated the classes as follows and your code works without any exceptions=>
class Mian:
public class Mian {
public static void main(String[] args) throws ClassNotFoundException {
URLConnectionReader urlcr = new URLConnectionReader();
String output =
urlcr.getUrlContents("https://www.tradegate.de/orderbuch_umsaetze.php?
isin=NO0010892359");
System.out.println(output);
}
}
and URLConnectionReader class:
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public String getUrlContents(String theUrl)
{
StringBuilder content = new StringBuilder();
try
{
URL url = new URL(theUrl);
URLConnection urlConnection = url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null)
{
content.append(line + "\n");
}
bufferedReader.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return content.toString();
}
}

How to read a simple playlist in java

I am trying to create a simple java command line code that will accept the URL from a playlist in command line and it should return the playlist content.
I am getting the following response back Enter playlist url here (0 to quit):
http://gv8748.lu.edu:8084/sweng987/simple-01/playlist.m3u8
java.net.MalformedURLException: no protocol: http://lu8748.lu.edu:8084/sweng987/simple-01/playlist.m3u8
at java.base/java.net.URL.<init>(URL.java:627)
at java.base/java.net.URL.<init>(URL.java:523)
at java.base/java.net.URL.<init>(URL.java:470)
at edu.lu.sweng987.SimplePlaylist.getPlaylistUrl(SimplePlaylist.java:36)
at edu.lu.sweng987.SimplePlaylist.main(SimplePlaylist.java:21)
My code is the following
package edu.psgv.sweng861;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.*;
public class SimplePlaylist {
private SimplePlaylist() {
//don't allow instances
}
// The main function returns the URL entered
public static void main(String[] args) throws IOException{
String output = getPlaylistUrl("");
System.out.println(output);
}
private static String getPlaylistUrl(String theUrl) {
String content = "";
Scanner scanner = new Scanner(System.in);
boolean validInput = false;
System.out.println("Enter playlist url here (0 to quit):");
content = scanner.nextLine();
try {
URL url = new URL(theUrl);
URLConnection urlConnection = (HttpURLConnection) url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
content += line + "\n";
}
bufferedReader.close();
} catch(Exception e) {
e.printStackTrace();
}
return content;
}
}
You have incorrectly used the parameter for the method when creating the URL instance instead of the local variable actually containing the url
Change
content = scanner.nextLine();
try {
URL url = new URL(theUrl);
to
content = scanner.nextLine();
try {
URL url = new URL(content);

Java Writing to a file: does not write to all desired files

I am out of ideas, I've been trying for the whole day to separate one file which has a format of :
AN Aixas
AN Aixirivall
AN Aixovall
AN Andorra la Vella
BR Salto do Mandira
BR Salto do Norte
BR Salto Dollman
BR Salto Grande
BR Salto Pilao
...
and so one, into different files by the name of the Country "AA.txt" and to include all the cities in these separate files. But my program only writes to a certain bunch of files and I cannot figure out why.
I've tried all the writing files classes - same result.
Here is the result, all worked but on a certain bunch of files only.
Here is the code :
package com.fileorganizer;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class File2 implements Cloneable {
static InputStream fis = null;
static BufferedReader br = null;
static String state = "";
static String tmp = "";
static File file = null;
static FileWriter fw = null;
static BufferedWriter bw = null;
public static void main(String[] args) {
int a = 0;
try {
fis = new FileInputStream(
new File(
"/Users/Mihail/Documents/WorkSpace/Parse-Starter-Project-1.8.2/ParseStarterProject/res/raw/cities.txt"));
br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while ((line = br.readLine()) != null) {
state = line.substring(0, 2);
if (state.substring(0, 1).matches("^[A-Z]+$")
&& state.substring(1, 2).matches("^[A-Z]+$")
&& !tmp.equals(state)) {
file = new File(
"/Users/Mihail/Documents/WorkSpace/Parse-Starter-Project-1.8.2/ParseStarterProject/res/raw/countriesFolder/"
+ state + ".txt");
fw = new FileWriter(file.getAbsoluteFile());
bw = new BufferedWriter(fw);
tmp = state;
}
bw.write(line.substring(3) + "\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (Exception ex) {
}
}
}
}
I am really sorry for such a dumb question. Please help
You don't close bw anywhere, so the contents in the BufferedWriter's buffer are lost.

Code deletes the content of the file rather than replacing a text

In my below code I wanted to replace the text "DEMO" with "Demographics" but instead of replacing the text it deletes the entire content of the text file.
Contents inside the file:
DEMO
data
morning
PS: I'm a beginner in java
package com.replace.main;
import java.io.*;
public class FileEdit {
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
String readLine, replacedData;
try {
bw = new BufferedWriter(
new FileWriter(
"Demg.ctl"));
br = new BufferedReader(
new FileReader(
"Demg.ctl"));
System.out.println(br.readLine()); //I Get Null Printed Here
while ((readLine = br.readLine())!= null) {
System.out.println("Inside While Loop");
System.out.println(readLine);
if (readLine.equals("DEMO")) {
System.out.println("Inside if loop");
replacedData = readLine.replaceAll("DEMO","Demographics");
}
}
System.out.println("After While");
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
You open a Writer to your file, but you don't write anything. This means that your file is replaced with an empty file.
Besides this you also need to close your writer, not just the reader.
And last but not least, your if condition is wrong.
if (readLine.equals("DEMO")) {
should read
if (readLine.contains("DEMO")) {
Otherwise it would only return true if your line contained "DEMO" but nothing else.
I'm updating the answer to my own question.
package com.replace.main;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileEdit
{
public static void main(String args[])
{
try
{
BufferedReader reader = new BufferedReader(new FileReader("Demg.ctl"));
String readLine = "";
String oldtext = "";
while((readLine = reader.readLine()) != null)
{
oldtext += readLine + "\r\n";
}
reader.close();
// To replace the text
String newtext = oldtext.replaceAll("DEMO", "Demographics");
FileWriter writer = new FileWriter("Demg.ctl");
writer.write(newtext);
writer.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

How do I get full sentences to print after reading in a text file?

I am trying to read in a technical paper, separate all the sentences, use a filter to find key terms and phrases in the sentences, and then create my own abstract.
What I have so far is two BufferedReaders reading a text file with a paragraph in it, and my filter being read in. Each line is then being stored into an ArrayList and printed to the console to test if they are being read correctly.
I want to know if I am approaching this the correct way by using a BufferedReader instead of a Scanner. I just want to be able to print out all the sentences after a '.' (dot), a '!' (exclamation-point), or a '?' (question-mark) for right now, so I know that the file is being read correctly.
This is my code so far:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.io.*;
import java.util.Scanner;
public class Filtering {
public static void main(String[] args) throws IOException {
ArrayList<String> lines1 = new ArrayList<String>();
ArrayList<String> lines2 = new ArrayList<String>();
try {
FileInputStream fstream1 = new FileInputStream("paper.txt");
FileInputStream fstream2 = new FileInputStream("filter2.txt");
DataInputStream inStream1 = new DataInputStream (fstream1);
DataInputStream inStream2 = new DataInputStream (fstream2);
BufferedReader br1 = new BufferedReader(
new InputStreamReader(inStream1));
BufferedReader br2 = new BufferedReader(
new InputStreamReader(inStream2));
String strLine1;
String strLine2;
while ((strLine1 = br1.readLine()) != null) {
lines1.add(strLine1);
}
while ((strLine2 = br2.readLine()) != null) {
lines2.add(strLine2);
}
inStream1.close();
inStream2.close();
}
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
System.out.println(lines1);
System.out.println(lines2);
}
}
It is a good practice to use a BufferedReader to read any File as it will buffer the File instead of accessing each bytes one by one
The DataInputStream is not needed
You should specify a character encoding in your InputStreamReader
You could accumulate all your string in a StringBuilder so that you have the whole text in a single reference
You may want to look into BreakIterator to split your text into sentences. Have a look at getSentenceInstance().
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.BreakIterator;
public class Filtering {
public static void main(String[] args) throws IOException {
File paperFile = new File("paper.txt");
File filterFile = new File("filter2.txt");
// If you want you could roughly initiate the stringbuilders to their
// approximate future size
StringBuilder paper = new StringBuilder();
StringBuilder filter2 = new StringBuilder();
FileInputStream fstream1 = null;
FileInputStream fstream2 = null;
try {
fstream1 = new FileInputStream(paperFile);
fstream2 = new FileInputStream(filterFile);
BufferedReader br1 = new BufferedReader(new InputStreamReader(fstream1, "UTF-8"));
BufferedReader br2 = new BufferedReader(new InputStreamReader(fstream2, "UTF-8"));
String strLine1;
String strLine2;
while ((strLine1 = br1.readLine()) != null) {
paper.append(strLine1).append('\n');
}
while ((strLine2 = br2.readLine()) != null) {
filter2.append(strLine2).append('\n');
}
}
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
} finally {
if (fstream1 != null) {
fstream1.close();
}
if (fstream2 != null) {
fstream2.close();
}
}
String paperString = paper.toString();
String filterString = filter2.toString();
System.out.println(paperString);
System.out.println(filterString);
// To break it into sentences
BreakIterator boundary = BreakIterator.getSentenceInstance();
boundary.setText(paperString);
int start = boundary.first();
for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
System.out.println(paper.substring(start, end));
}
}
}

Categories

Resources