Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
java_dzienna_15_09
Commits
4c2aff1d
Commit
4c2aff1d
authored
Sep 15, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Przykłady bibliotek Apache Commons
parent
1548b070
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
113 additions
and
0 deletions
+113
-0
pom.xml
PC21-Maven/pom.xml
+12
-0
CommonsLang.java
PC21-Maven/src/main/java/biblioteki/CommonsLang.java
+77
-0
SkopiujKatalog.java
PC21-Maven/src/main/java/pliki/SkopiujKatalog.java
+24
-0
No files found.
PC21-Maven/pom.xml
View file @
4c2aff1d
...
@@ -10,4 +10,16 @@
...
@@ -10,4 +10,16 @@
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
</properties>
<dependencies>
<dependency>
<groupId>
commons-io
</groupId>
<artifactId>
commons-io
</artifactId>
<version>
2.11.0
</version>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-lang3
</artifactId>
<version>
3.12.0
</version>
</dependency>
</dependencies>
</project>
</project>
PC21-Maven/src/main/java/biblioteki/CommonsLang.java
0 → 100644
View file @
4c2aff1d
package
biblioteki
;
import
org.apache.commons.lang3.ObjectUtils
;
import
org.apache.commons.lang3.StringUtils
;
public
class
CommonsLang
{
public
static
void
main
(
String
[]
args
)
{
String
[]
t
=
{
null
,
"Ala"
,
"Agnieszka"
,
"Zenon"
,
"Żaneta"
,
"Łukasz"
,
"Łucja"
,
"ala"
,
"łukasz"
,
""
,
" "
};
// Gdy w Javie porównujemy Stirngi (i inne obiekty), robimy to za pomocą equals.
// Gdybyśmy próbowali wywołać equals na zmiennej, która może by nullem, to doprowadzimy do błędu NPE
// for(int i = 2; i >= 0; i--) {
// if(t[i].equals("Ala")) {
// System.out.println(i + " to jest Ala");
// } else {
// System.out.println(i + " to nie jest Ala");
// }
// }
// Dlatego zalecane jest, aby zmienne porównywać z konkretnymi napisami w tej kolejności:
// "Napis".equals(zmienna)
// a nie zmienna.equals("Napis")
for
(
int
i
=
2
;
i
>=
0
;
i
--)
{
if
(
"Ala"
.
equals
(
t
[
i
]))
{
System
.
out
.
println
(
i
+
" to jest Ala"
);
}
else
{
System
.
out
.
println
(
i
+
" to nie jest Ala"
);
}
}
// Jednak czasami porównujemy dwie zmienne, z których obie mogą być nullem, wtedy jest trudniej.
// Biblioteka commons-lang i klasa StringUtils pomaga w operacjach porównywania, sprawdzania czy napis jest pusty itd.
System
.
out
.
println
();
// Porównania stringów, które są odporne na wartości null (nie pojawiają się błędy)
System
.
out
.
println
(
StringUtils
.
equalsIgnoreCase
(
t
[
0
],
t
[
7
]));
// false, bo null != ala
System
.
out
.
println
(
StringUtils
.
equalsIgnoreCase
(
t
[
1
],
t
[
7
]));
System
.
out
.
println
(
StringUtils
.
equalsIgnoreCase
(
t
[
5
],
t
[
8
]));
// Łukasz == łukasz
System
.
out
.
println
();
System
.
out
.
println
(
StringUtils
.
isEmpty
(
t
[
0
]));
// tak, bo jest nullem
System
.
out
.
println
(
StringUtils
.
isEmpty
(
t
[
9
]));
// tak, bo jest pustym stringiem
System
.
out
.
println
(
StringUtils
.
isEmpty
(
t
[
10
]));
// false, bo zawiera 3 spacje
System
.
out
.
println
(
StringUtils
.
isBlank
(
t
[
10
]));
System
.
out
.
println
(
StringUtils
.
isBlank
(
t
[
0
]));
System
.
out
.
println
(
StringUtils
.
isBlank
(
t
[
1
]));
System
.
out
.
println
();
// są też wersje dla wielu wartości na raz
System
.
out
.
println
(
StringUtils
.
isAllBlank
(
t
[
0
],
t
[
9
],
t
[
10
]));
System
.
out
.
println
(
StringUtils
.
isAllBlank
(
t
[
0
],
t
[
8
],
t
[
10
]));
// praktyczne zastosowanie: walidacja danych wprowadzonych przez użytkownika:
String
imie
=
"Ala"
,
nazwisko
=
"Kowalska"
,
adres
=
" "
;
if
(
StringUtils
.
isAnyBlank
(
imie
,
nazwisko
,
adres
))
{
System
.
out
.
println
(
"Nie podałeś danych"
);
}
else
{
// robimy coś ważnego
}
System
.
out
.
println
();
for
(
String
s
:
t
)
{
if
(
StringUtils
.
isNotEmpty
(
s
))
{
System
.
out
.
println
(
"|"
+
StringUtils
.
center
(
s
,
20
,
'.'
)
+
"|"
);
}
}
System
.
out
.
println
();
System
.
out
.
println
(
ObjectUtils
.
firstNonNull
(
t
));
}
}
PC21-Maven/src/main/java/pliki/SkopiujKatalog.java
0 → 100644
View file @
4c2aff1d
package
pliki
;
import
java.io.File
;
import
java.io.IOException
;
import
org.apache.commons.io.FileUtils
;
public
class
SkopiujKatalog
{
// W standardowej bibliotece Javy nie ma operacji, która kopiowałaby na dysku cały katalog z plikami.
// W tym przykładzie użyjemy biblioteki Apache Commons IO, która taką operację zawiera.
public
static
void
main
(
String
[]
args
)
{
try
{
File
zrodlo
=
new
File
(
"src/main/java"
);
File
cel
=
new
File
(
"/home/patryk/Pulpit/zrodla"
);
FileUtils
.
copyDirectory
(
zrodlo
,
cel
);
System
.
out
.
println
(
"Gotowe"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment