I'd like to know what practical way of constructing reports for EPSON Dot Matrix printers exists in Java. At this time, I'm having the LX300+II model to play with.
I know that there are basically two ways of using this printer:
As a typewriter, outputting directly raw ASCII data to the parallel port
Graphical printing, with graphical fonts and precise positioning.
How can I use both fast printing fonts(provided by 1) and precise positioning (provided by 2)?
I know this is possible to do because a couple of years ago, I got to make reports for the EPSON FX 2180 that included drivers with native printing fonts installed in Windows. This allowed to do exactly what I want here.
Now I'm using JasperReports for graphical reporting and works fine, but I do have some reports that need to be printed in dot matrix printers and fast, too.
What can be an alternative for that?
Would TextPrinter fit your needs?
If you want to print fast in dot-matrix printers, you need to do it in "plain-text" mode. The following code works for me:
try {
// LPT1 is the printer port
try (FileWriter out = new FileWriter("LPT1:")) {
out.write("String1\nString2\nString3\n");
out.flush();
}
} catch (IOException e) {
}
//java print with printer dot matrix
String bill = "your text";
InputStream br = new ByteArrayInputStream(bill.getBytes());
BufferedReader in = new BufferedReader(new InputStreamReader(br));
String line;
//if you use windows
FileWriter out = new FileWriter("////IP Printer//printer name");
//if you use linux you can try SMB:(samba)
while((line = in.readLine()) != null)
{
System.out.println("line"+line);
out.write(line);
out.write(0x0D); CR
out.write('\n');
writer.println(line);
}
out.close();
in.close();
writer.close();
//it work for me...
Related
I have a requirement to send the sms and email on the trigger of some event.
It's working fine when I am testing with english. But when I change the text to Japanese, it's producing some sort of junk message. I am using java as my programming language.
I have tried some solutions like changing the charset preference and adding -Dfile.encoding=UTF8 in run configurations, but doesn't seem to work.
It's not working in a particular case.
When I hard-code the string in my java class as Japanese string then it's working fine. But when I try to read from property file it's producing some junk characters.
Finally I have solved this. As I have stated in my question itself, that the problem is mainly with reading from .property file. When I tried to hardcode the string it's working perfectly.
As #Henry suggested in his comments, all the .property files contain only ISO8859-1 characters. So, I followed the process to convert string from ISO8859-1 format to UTF.
It can be achieved by simply using this one line of code.
String utf8String = new String(Charsets.ISO_8859_1.encode("your string").array()).
Although, It solved my purpose, But I thought it's not a clean way to solve this.
For my scenario I had to add a new configuration, so instead of going through above way. I kept the string in separate text file and read from the file.
String filePath = "filename.txt"
try {
BufferedReader br = new BufferedReader(new FileReader(filePath));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
return sb.toString();
} catch (FileNotFoundException e) {
log.error("file not found ", e);
} catch (IOException e) {
log.error("Error occurred ", e);
}
It solved the issue without using any hack to convert from one CharSet to another.
I have designed a GUI in Java using Swing.
Using GUI I read the location as inputs
Using these inputs as parameter I call a Python Script from this Java Code
Now, I need to display the output of the python script on GUI dynamically.
As the python script runs the output log of the script has to be displayed on the GUI area simultaneously.
Is there anyway I can do that ?
Please help
A code would be useful, however, you could write the python script output on a file and than read that output from that file to the Java GUI.
Python
out_file = open("test.txt","w")
out_file.write("This Text is going to out file\nLook at it and see\n")
out_file.close()
JAVA
File name = new File("C:/path/test.txt");
if (name.isFile()) {
try {
BufferedReader input = new BufferedReader(new FileReader(name));
StringBuffer buffer = new StringBuffer();
String text;
while ((text = input.readLine()) != null){
buffer.append(text + "\n");}
input.close();
System.out.println(buffer.toString());
} catch (IOException ioException) {}
}
I am new to java Network programming.I was googling the code for a TCP client in java.I came across the following example.
import java.lang.*;
import java.io.*;
import java.net.*;
class Client {
public static void main(String args[]) {
try {
Socket skt = new Socket("localhost", 1234);
BufferedReader in = new BufferedReader(new
InputStreamReader(skt.getInputStream()));
System.out.print("Received string: '");
while (!in.ready()) {}
System.out.println(in.readLine()); // Read one line and output it
System.out.print("'\n");
in.close();
}
catch(Exception e) {
System.out.print("Whoops! It didn't work!\n");
}
}
}
The client seems to read out the data one "line" at a time?. I am connecting to a server that is streaming OpenFlow packets.A wireshark screenshot of OpenFlow packets is given below.
[http://www.openflow.org/downloads/screenshot-openflow-dissector-2008-07-15-2103.jpg][1]
Once I recieve the complete packets I want to dump that to a file and then later read it using wireshark for example.In the above code they are using calss BufferedReader to read the data in "lines"? At least that is how I understand it.Is there someway in which I can get full packets and then write it to the file?
Readers are for working with text data. If you are working with binary data (it's not entirely clear from that screenshot), you should be working with some type of Stream (either InputStream or possibly DataInputStream). Don't just look for random examples on online, try to find ones that actually apply to what you are interested in doing.
also, don't ever use InputStream.available, it's pretty much useless. as is any example code using it.
also, a simple google search for "OpenFlow java" had some interesting hits. are you sure you need to write something from scratch?
No, but there are libraries that provides such functions. See for example Guava
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/ByteStreams.html
If you don't want to (or can't) use libraries you shoud consume a stream like this
List<String> lst = new ArrayList<String>();
String line;
while ((line = in.readLine()) != null) {
lst.add(line);
}
or
String str = "";
String line;
while ((line = in.readLine()) != null) {
str += line + "\n";
}
Note that the BufferedReader.readLine() method will give you a new line on linebreaks ('\n'). If the InputStream is binary you should work with bytes instead.
I've made an Applet Search Utility in which I provide a string as input and find that string in the specified file or folder.
I've done with this but I m not happy with its performance.
The process is taking too much time to respond.
I decided to do its profiling to see what is happening and I noticed that the method scanner.hasNextLine() is taking most of the time.
Though this is very important method for my program because I have to read all the lines and find that string, Is there any other way by which I can improve its performance and reduce the execution time
Here is the code where I am using this method ....
fw = new FileWriter("filePath", true);
bw = new BufferedWriter(fw);
for (File file : filenames) {
if(file.isHidden())
continue;
if (!file.isDirectory()) {
Scanner scanner = new Scanner(file);
int cnt = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(!exactMatch)
{
if(!caseSensitive)
{
if (line.toLowerCase().contains(searchString.toLowerCase())) {
// System.out.println(line);
cnt += StringUtils.countMatches(line.toLowerCase(),
searchString.toLowerCase());
}
}
else
{
if (line.contains(searchString)) {
// System.out.println(line);
cnt += StringUtils.countMatches(line,
searchString);
}
}
}
And yes the method toLowerCase() is also taking more time then expected.
I have changed my code and now I am using BufferedReader in place of Scanner as Alex and Nrj suggested and I found a nice improvement in the performance of my application.
It is now processing in one third time of its earlier version.
Thanks to all that replied.....
Following your question I examined code of Scanner and I think that your are right. It is not optimized to work with large data. I'd recommend you to use simple BufferedReader that wraps InputStreamReader that wraps FileInputStream:
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)))
then read line-by-line:
r.readLine()
If this is not enough for you try to read bulks of lines and then process them.
Concerning to toLowerCase() you can try to use regular expressions instead. The benefit is that you do not have to change the case of line every time. The disadvantage is that in simple cases regular expression works a bit slower than regular string comparison.
I would suggest redesigning your solution and use something like Lucene to do the search for you. You can index and search files with Lucene much more efficiently, tutorial on how to do it with text files can be found here: http://www.avajava.com/tutorials/lessons/how-do-i-use-lucene-to-index-and-search-text-files.html
(Only small optimizations, in response to comment above.)
if(!caseSensitive)
{
searchString = searchString.toLowerCase();
}
while (true) {
String line = bufferedReader.readLine();
if (line == null)
break;
if(!caseSensitive)
{
line = line.toLowerCase();
}
if(!exactMatch)
{
if (line.contains(searchString)) {
// System.out.println(line);
cnt += StringUtils.countMatches(line,
searchString);
}
}
Try using BufferedReader
Make use of threads. You can search the files in parallel which should reduce the search time.
I would not use Java to search the file system for matches of the string. Instead invoke a native algorithm from Java instead. I would invoke grep from Java using something like this:
ProcessBuilder pb = new ProcessBuilder("grep", "-r", "foo");
pb.directory(new File("myDir"));
Process p = pb.start();
InputStream in = p.getInputStream();
//Do whatever you prefer with the stream
I've created a java application I'm selling for money, and the verification system involves using an unique HWID to ID the computer to see if they've paid. I was wondering if there was a way for a java application to "kill" itself, maybe deleting some of it's own class files, corrupting itself, or overriding itself.
Is there any way?
Make it web based, keep records in the database, make the user log in to use the system. Any dedicated cracker will defeat your system in a matter of time.
If this is a commercial grade app, then I would recommend using a security solution designed by professionals. Security and Cryptography is best left to experts
Layman solution :
Could you execute a getmac (assuming this app runs out of windows) from within your system and do the check.? MAC ids are assumed to be unique for a PC. There are ways to override it but should address 90% of the cases.
Corrupting your app doesn't seem to be a good solution.
public static String getURLSource(String link) {
try {
URL url = new URL(link);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder str = new StringBuilder();
String inputLine;
while ((inputLine = reader.readLine()) != null) {
str.append(inputLine);
}
reader.close();
return str.toString();
} catch (Exception e) {
throw new RuntimeException("Couldn't properly connect to internet.");
}
}
public void main(String[] args) {
if(!getUrlSource("yourlink").contains("a string you want when it's not killswitched")) { //The link must be readable text by java
//Do stuff here
}
}